我有一个数字表,用户应该能够在其中编辑值并在提交时更新数据库。
到目前为止,我的错误代码在提交时使用值 1 更新每个字段,无论输入了什么。
提交代码:
//If the confirm button has been hit:
if (isset($_POST['submit'])) {
//Create the foreach loop
foreach($_POST['classtoupdate'] as $classes){
//Grab the POST data and turn it into integers
$class_id = (int)$classes;
$totalspcs = (int)$_POST['allspaces'];
$totalbkgs = (int)$_POST['allbookings'];
$newbies = (int)$_POST['noobs'];
//Change the booking numbers:
$newdeets = "UPDATE classes SET total_spaces = '$totalspcs', total_bookings = '$totalbkgs', free_spaces = ('$totalspcs' - '$totalbkgs'), newbies = '$newbies' WHERE class_id = '$class_id')";
echo $newdeets;
mysqli_query($dbc, $newdeets);
}
mysqli_close($dbc);
echo 'All good, yay! <a href="admin.php">Go look</a>';
}
形式:
//create the form
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" >';
echo '<tr><td>' . $class . '</td>';
echo'<td>' . $new_date . '</td>';
echo '<td>' . $new_time . '</td>';
echo '<td><input type="text" maxlength="2" class="input-mini" name="noobs[]" id="noobs[]" value="' . $newbies . '">';
echo '<td><input type="text" maxlength="2" class="input-mini" name="allspaces[]" id="allspaces[]" value="' . $totalspaces . '">';
echo '<td><input type="text" maxlength="2" class="input-mini" name="allbookings[]" id="allbookings[]" value="' . $bookings . '"
>';
echo '<td>' . $freespaces . '</td>';
echo' <input type="hidden" name="classtoupdate[]" id="classtoupdate[]" value="' . $class . '" />';
}
echo'</table>';
// Make booking button
echo '<input type="submit" name="submit" class="btn btn-large btn-primary pull-right" value="Update">';
echo '</form>';
输入随机值(非 1)后的回显查询结果如下:
UPDATE classes SET total_spaces = '1', total_bookings = '1', free_spaces = ('1' - '1'), newbies = '1' WHERE class_id = '26')
UPDATE classes SET total_spaces = '1', total_bookings = '1', free_spaces = ('1' - '1'), newbies = '1' WHERE class_id = '16')
..等等每个表格行。在对 SO 和手册进行广泛搜索后,我找不到复制的问题。
我在 POST 结果上尝试了 intval()、serialize 和 array_map(可能不正确);我尝试了不同的 foreach 循环将它们转换为整数,但仍然没有乐趣。
有什么建议吗?