gpt4 book ai didi

php - 将foreach循环中的多个数组POST值插入到mysql中

转载 作者:行者123 更新时间:2023-11-29 22:13:22 24 4
gpt4 key购买 nike

<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>

<?php foreach($teacher_names as $name): ?>

<tbody>
<tr>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="f_name" value="<?php echo $name['first_name']; ?>"></td>
<td class="col-md-2"><input class="form-control" type="text" readonly="readonly" name="l_name" value="<?php echo $name['last_name']; ?>"></td>
<td class="col-md-2"><input type="checkbox" name="present" value="Y"></td>
<td class="col-md-2"><input type="checkbox" name="absent" value="N"></td>

</tr>
</tbody>
<?php endforeach;?>
</table>

我希望能够访问数据库中的所有值,目前为 10。我想获取所有 $_POST 值,然后将它们插入数据库,但似乎我只获取 foreach 循环中的一个 POST 值,即最后一个键值。 php 脚本的示例是

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";
}

}

我只是希望能够将所有 POST 值和 checkbox 值访问到一个数组中,然后将它们全部插入数据库中。如果我能找到解决这个问题的方法,我将不胜感激。

最佳答案

由于在未选中复选框时第一次没有点击关于不匹配键的信息,因此我编写了另一种方法来完成您所追求的任务。

因此您的模板应如下所示:

<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>

然后从 0 开始,每次加 1,循环直到不再找到编号的名称字段(此时假设不再有条目)

<?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++;
}

?>

关于php - 将foreach循环中的多个数组POST值插入到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31419436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com