【发布时间】:2017-03-15 06:40:48
【问题描述】:
我是 PHP 编程新手。
我正在开发一个 PHP 项目,我正在从 mysql 数据库中填充一个动态表,其中每一行都有一个复选框字段。
<?php
$sql = "select * from caseinstruction where fir_nos='$fir_nos'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$i = 1;
while($row = $result->fetch_assoc()) {
echo "<tr><td class='text-center'>". $i++ ."</td>
<td><input type='text' value='{$row['Instructions']}' class='form-control input-sm' style='border:none'></td>
<td class='text-center form-group'><input type='checkbox' name='foo[]' value='1'></td>
</tr>\n";
}
}
?>
它运行良好。当我想用选中的值更新同一个 mysql 表时,就会出现问题。假设如果用户选择了一个复选框,那么它将在 mysql 表中反映 1,如果没有选择任何选项,则它将在 mysql 表中反映 0。
我绑定运行 foreach 命令,它会按预期填充值,但是当我运行更新命令时,它只反映 0。请帮助我。
<?php
require('../php-includes/connect.php');
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$fir_nos = $_POST['fir_nos'];
$cins = $_POST['foo'];
echo '<pre>';
print_r($cins);
echo '</pre>'
foreach ($$cins as $value) {
$sql="update mytable set report02='$value'";
$result = $con->query($sql);
}
}
我在这方面遗漏了一些东西。请帮我。提前谢谢大家。
# | Instruction | value |
-------------------------
1 | abc | 1 | //checked
2 | cdf | 0 | //unckecked
3 | efg | 1 | //checked
-------------------------
【问题讨论】: