Php 将foreach循环中的多个数组POST值插入mysql
我只希望能够访问数组中的所有POST值和复选框值,然后将它们全部插入数据库。如果我能找到一些解决这个问题的方法,我将不胜感激。因为在没有选中复选框的情况下,它没有第一次与我一起单击“取消匹配键”,所以我写了另一种方法来完成您所要的 因此,您的模板应如下所示:Php 将foreach循环中的多个数组POST值插入mysql,php,html,mysql,Php,Html,Mysql,我只希望能够访问数组中的所有POST值和复选框值,然后将它们全部插入数据库。如果我能找到一些解决这个问题的方法,我将不胜感激。因为在没有选中复选框的情况下,它没有第一次与我一起单击“取消匹配键”,所以我写了另一种方法来完成您所要的 因此,您的模板应如下所示: public function teachers_attendance($first_name, $last_name, $present, $absent){ $sql = "INSERT
public function teachers_attendance($first_name, $last_name, $present, $absent){
$sql = "INSERT INTO teacher_attendance($first_name, $last_name, $present, $absent)
VALUES(:first_name, :last_name, :present, :absent)";
$stmt = $this->_connection->prepare($sql);
$stmt->bindValue(':first_name', $first_name, PDO::PARAM_INT);
$stmt->bindValue(':last_name', $last_name, PDO::PARAM_STR);
$stmt->bindValue(':present', $present, PDO::PARAM_STR);
$stmt->bindValue(':absent', $absent, PDO::PARAM_STR);
$stmt->Execute();
$num_rows = $stmt->rowCount();
if($num_rows > 0){
echo "Yes";
}else{
echo "No";
}
}
然后从0开始,每次添加1,循环直到不再找到编号的名称字段,此时假定不再有更多条目
<table class="table table-bordered">
<thead>
<tr>
<th class="col-md-2">First Name</th>
<th class="col-md-2">Last Name</th>
<th class="col-md-2">Present</th>
<th class="col-md-2">Absent</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach($teacher_names as $name):
?>
<tr>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="f_name_<?php echo $i; ?>" value="<?php echo $name['first_name']; ?>" /></td>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="l_name_<?php echo $i; ?>" value="<?php echo $name['last_name']; ?>" /></td>
<td class="col-md-2"><input type="checkbox" name="present_<?php echo $i; ?>" value="Y" /></td>
<td class="col-md-2"><input type="checkbox" name="absent_<?php echo $i; ?>" value="N" /></td>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
乍一看,我认为循环中表单元素的name属性是问题所在。name属性是$\u POST引用的内容,在循环HTML中,它们都是相同的,这将导致一个属性覆盖下一个属性,以此类推,只剩下最后一个属性集。将循环每次迭代的唯一值附加到这些字段的name属性可能会解决您的问题。由于未选中的复选框不会被发送,您将遇到isset$\u POST['present'][$i]/isset$\u POST['缺席'][$i]问题,因为您将有不匹配的键。例如,您有10$_POST['f_name'],其中5人出席,5人缺席$_POST['present'][4]和$_POST['缺席][4]是您将获得的最大值,因此您将无法获得与$_POST['f_name'][9]相匹配的内容。是的,您说得对,肖恩,您建议我该怎么做it@user3769234我已经修改了我的答案,加入了一个应该考虑到肖恩指出的问题的解决方案,谢谢你指出,肖恩!
<table class="table table-bordered">
<thead>
<tr>
<th class="col-md-2">First Name</th>
<th class="col-md-2">Last Name</th>
<th class="col-md-2">Present</th>
<th class="col-md-2">Absent</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach($teacher_names as $name):
?>
<tr>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="f_name_<?php echo $i; ?>" value="<?php echo $name['first_name']; ?>" /></td>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="l_name_<?php echo $i; ?>" value="<?php echo $name['last_name']; ?>" /></td>
<td class="col-md-2"><input type="checkbox" name="present_<?php echo $i; ?>" value="Y" /></td>
<td class="col-md-2"><input type="checkbox" name="absent_<?php echo $i; ?>" value="N" /></td>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
<?php
// First row to check is 0, as per template
$i = 0;
// Loop while f_name_* and l_name_* are found (if not found, it is assumed there are no more rows)
while( isset( $_POST['f_name_'.$i] ) && isset( $_POST['l_name_'.$i] ) )
{
// Get and set name variables (based on current row)
$first_name = $_POST['f_name_'.$i];
$last_name = $_POST['l_name_'.$i];
// Presence and absence are checkboxes with a Y value, so we'll set these to 'N' if not found
$present = ( isset( $_POST['present_'.$i] ) ? $_POST['present_'.$i] : 'N' );
$absent = ( isset( $_POST['absent_'.$i] ) ? $_POST['absent_'.$i] : 'N' );
// Call the teachers_attendance function
teachers_attendance($first_name, $last_name, $present, $absent);
// Increment the row to check
$i++;
}
?>