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

时间:2013-08-21 10:10:08

标签: 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 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循环将它们转换为整数,仍然没有快乐。

有什么建议吗?

5 个答案:

答案 0 :(得分:0)

$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 :(得分:0)

你的POST变量是数组(例如allspaces [])

所以,这个赋值总是给你一个结果,因为你试图将数组转换为整数。

$totalspcs = (int)$_POST['allspaces']; 

你应该循环你的数组并以不同的方式使用数据。 如果您不需要数组,请删除输入名称中的方括号。

答案 2 :(得分:0)

试试这个:

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

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

请注意每行中的$index

答案 3 :(得分:0)

问题在于您为输入字段命名的方式。 <input type="text" maxlength="2" class="input-mini" name="noobs[]"> []意味着数据将作为数组发布回服务器,并且您将数组类型转换为整数,这就是为什么每个地方都获得1作为值。失去[],例如。 <input type="text" maxlength="2" class="input-mini" name="noobs">应该解决问题。

答案 4 :(得分:0)

我举了一个例子,你应该可以用它来解决你的问题。

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

您遇到的问题是,即使您正在循环$_POST['classtoupdate'],您仍在使用$_POST['allbookings']和其他仍为数组的字段。

如果数组不为空,则将数组转换为整数值将始终返回1。所以你必须从中提取值。

相关问题