将POST数组插入到mySql表的多行中的问题[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有一个表格,用于将高尔夫球场记分卡输入数据库

<table class="table table-bordered text-center table-striped">
<col style="width:15%">
<col style="width:20%">
<col style="width:20%">
<col style="width:20%">

<thead >
<tr>
  <th>Hole</th>
  <th>Yards</th>
  <th>Par</th>
  <th>SI</th>
    </tr>
</thead>

<tbody>
<?php

      for ($i = 1; $i <= 2; $i++) {
        echo 
    "<tr>

    <td ><input type='number' class='form-control' 
    name='holeNumber[]' id='hole".$i."iD' value=".$i." readonly> 
    </td>


    <td ><input type='number' class='form-control' 
    name='holeYards[]' id='hole".$i."yards'  ></td>


    <td ><input type='number' class='form-control' name='holePar[]' 
    id='hole".$i."par'  ></td>


    <td ><input type='number' class='form-control' 
    name='holeStrokeIndex[]' id='hole".$i."strokeIndex'  ></td>

</tr>";
}
    ?>

    </tbody>
    </table>

我只设置它显示2行以方便测试

表单在4个数组中正确提交我需要的数据,现在我需要将这些数据放入2个单独的行中。

mySql代码是

 $holeNumber = $_POST['holeNumber'];
 $yards = $_POST['holeYards'];
 $par = $_POST['holePar'];
 $strokeIndex = $_POST['holeStrokeIndex'];

    $sqlHoles = "INSERT INTO holes (courseId, holeNumber, yards, 
                 par, strokeIndex) VALUES (?, ?, ?, ?, ?)";


      if($stmtHole = mysqli_prepare($link, $sqlHoles)){

  // Bind variables to the prepared statement as parameters
      for ($i=0; $i<=1 ; $i++){

*   $holeNumber = $holeNumber[$i];
*   $yards      = $yards[$i];
*   $par        = $par[$i];
*   $strokeIndex= $strokeIndex[$i];

    mysqli_stmt_bind_param($stmtHole, "iiiii", $holeNumber, 
           $yards, $par, $strokeIndex);

}


   if(mysqli_stmt_execute($stmtHole)){
    echo "holes added to database.";
    } else{
    echo "ERROR: Could not execute query: $sqlHoles. " . 
    mysqli_error($link);
    }
    } else{
         echo "ERROR: Could not prepare query: $sqlHoles. " . 
      mysqli_error($link);
     }

我现在正在接受

未初始化的字符串偏移:1错误

与标有*的线有关。

我意识到它与数组有关,花了很长时间在线寻找答案,但我迷失了我需要做的事情!

php mysql prepared-statement
1个回答
1
投票

当你执行$holeNumber = $holeNumber[$i];时,重新初始化变量并将其从数组更改为整数。只需将$ holeNumber更改为新变量$ intHoleNumber或其他内容。 (当然还有其他变量)。然后在bind函数中使用新变量。

© www.soinside.com 2019 - 2024. All rights reserved.