【问题标题】:How can $_POST pass input variables to MySQL query as integers, not 1?$_POST 如何将输入变量作为整数而不是 1 传递给 MySQL 查询?
【发布时间】:2013-08-23 15:50:16
【问题描述】:

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

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

有什么建议吗?

【问题讨论】:

  • 在您的表单中,您使用的是name="noobs[]"。删除 [] 并重试(适用于所有字段)。
  • 对不起,我没有看到你第一次尝试处理数组,请看下面我的回答。

标签: php mysql arrays forms post


【解决方案1】:

试试这个:

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

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

注意每一行中的$index

【讨论】:

    【解决方案2】:
    $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++;
    }
    

    【讨论】:

    • 看起来不错 - 为了简洁起见,我使用了 Hereblur 的类似答案。
    【解决方案3】:

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

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

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

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

    【讨论】:

      【解决方案4】:

      问题在于您命名输入字段的方式。 &lt;input type="text" maxlength="2" class="input-mini" name="noobs[]"&gt; [] 表示数据将作为数组回发到服务器,并且您正在将数组类型转换为整数,这就是为什么在任何地方都会得到 1 作为值。丢失[],例如。 &lt;input type="text" maxlength="2" class="input-mini" name="noobs"&gt; 应该可以解决问题。

      【讨论】:

        【解决方案5】:

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

        <?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。所以你必须从中提取值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-11-12
          • 2016-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-19
          • 1970-01-01
          相关资源
          最近更新 更多