0

I have a form for inputting golf course scorecard into database

<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>

I have only set it to show 2 rows to ease testing

The form submits the data i need correctly in 4 arrays, and i now need to get these into the 2 separate rows.

The mySql code is

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

I'm currently getting a

Uninitialized string offset: 1 error

relating to lines marked with an *.

I realise its something to do with the array and spent ages looking for an answer online but i'm getting lost as to what i need to do!

Jon
  • 91
  • 1
  • 10
  • You could just bind directly to the array values: `mysqli_stmt_bind_param($stmtHole, "iiiii", $holeNumber[$i], $yards[$i], $par[$i], $strokeIndex[$i]);` to resolve this problem – Nick Mar 30 '19 at 23:39

1 Answers1

2

When you do $holeNumber = $holeNumber[$i]; you reinitialize the variable and changes it from an array to an integer. Just change $holeNumber to a new variable $intHoleNumber or something. (And of course the other variables as well). Then use the new variables in the bind function.

aanders77
  • 620
  • 8
  • 22