Php $\u如何将输入变量作为整数而不是1传递给MySQL查询?
我有一个数字表,用户应该能够编辑值,并在提交时更新数据库 到目前为止,我的错误代码在提交时用值1更新每个字段,不管输入了什么 提交时的代码:Php $\u如何将输入变量作为整数而不是1传递给MySQL查询?,php,mysql,arrays,forms,post,Php,Mysql,Arrays,Forms,Post,我有一个数字表,用户应该能够编辑值,并在提交时更新数据库 到目前为止,我的错误代码在提交时用值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
//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>';
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()、序列化和数组映射(可能不正确);我尝试了不同的foreach循环来将它们转换成整数,但仍然没有乐趣
有什么建议吗?您的POST变量是数组(例如allspaces[])
$i=0;
//Create the foreach loop
foreach($_POST['classtoupdate'][$i] as $classes){
//Grab the POST data and turn it into integers
$class_id = (int)$classes;
$totalspcs = (int)$_POST['allspaces'][$i];
$totalbkgs = (int)$_POST['allbookings'][$i];
$newbies = (int)$_POST['noobs'][$i];
//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);
$i++;
}
所以,这个赋值的结果总是为1,因为您试图将数组强制转换为整数
$totalspcs = (int)$_POST['allspaces'];
您应该循环数组并以不同的方式使用数据。
如果不需要数组,请去掉输入名称中的方括号。试试以下方法:
foreach($_POST['classtoupdate'] as $index => $classes){
及
请注意每行中的
$index
。问题在于输入字段的命名方式<代码>该[]
意味着数据将作为数组发回服务器,并且您正在将数组类型转换为整数,这就是为什么您在任何地方都会得到1作为值的原因。丢失[]
,例如
应该可以解决问题。我举了一个例子,你应该可以用它来解决问题
<?php
$foo = array('1','2','3');
$bar = array('4','5','6');
// Simulates the $_POST variable
$baz = array('foo'=>$foo, 'bar'=>$bar);
// You should ensure that all entries have an equal length before iterating through!
if(count($baz['foo']) === count($baz['bar'])){
foreach($baz['foo'] as $key=>$value){
$x = (int)$value;
$y = (int)$bar[$key];
var_dump($x, $y);
}
}
?>
您遇到的问题是,即使您在$\u POST['classtoupdate']
中循环,您仍然在使用$\u POST['allbookings']
和其他仍然是数组的字段
如果数组不是空的,将数组转换为整数值将始终返回1。因此,您必须从中提取值。在表单中,您使用的是
name=“noobs[]”
。删除[]
,然后重试(对于所有字段)。抱歉,我没有看到您第一次尝试处理数组,请参阅下面的答案。看起来不错-我使用Hereblur的类似答案只是为了简洁。
<?php
$foo = array('1','2','3');
$bar = array('4','5','6');
// Simulates the $_POST variable
$baz = array('foo'=>$foo, 'bar'=>$bar);
// You should ensure that all entries have an equal length before iterating through!
if(count($baz['foo']) === count($baz['bar'])){
foreach($baz['foo'] as $key=>$value){
$x = (int)$value;
$y = (int)$bar[$key];
var_dump($x, $y);
}
}
?>