gpt4 book ai didi

php - $_POST 如何将输入变量作为整数而不是 1 传递给 MySQL 查询?

转载 作者:行者123 更新时间:2023-11-30 01:22:52 25 4
gpt4 key购买 nike

我有一个数字表,用户应该能够在其中编辑值并在提交时更新数据库。

到目前为止,我的错误代码在提交时将每个字段更新为值 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 循环将它们转换为整数,但仍然没有乐趣。

有什么建议吗?

最佳答案

试试这个:

foreach($_POST['classtoupdate'] as $index => $classes){

$totalspcs = (int)$_POST['allspaces'][$index]; 
$totalbkgs = (int)$_POST['allbookings'][$index];
$newbies = (int)$_POST['noobs'][$index];

注意每行中的$index

关于php - $_POST 如何将输入变量作为整数而不是 1 传递给 MySQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18354629/

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