0

I have a list that is populated from a database table as below.

<?php
while($row=mysql_fetch_array($result))
{
  ?>
    <tr>
      <td>
        <input type="checkbox" name="name[]" value="<?php echo 
$row['AnalysisID']; ?>">
    <?php echo $row["CustomerName"]; ?>
    <input type="hidden" name="Site[]" value="<?php echo $row["Site"]; ?>">
    <input type="hidden" name="Unit[]" value="<?php echo $row["Unit"]; ?>">
  </td>
  <td><?php echo $row['Site']; ?></td>
  <td><?php echo $row['Unit']; ?></td>
</tr>
<?php
}
?>

This displays the data perfectly, and in inspect all data is correct.

However I want to be able to check the various check-boxes and insert those rows to another table.

to achieve this I am using:

$counter = count($_POST["name"]);

for($x=0; $x<=$counter; $x++){

 if(!empty($_POST["name"][$x])){
  $PI_NO = mysql_real_escape_string($_POST["name"][$x]);
  $CUSTOMER_NAME = mysql_real_escape_string($_POST["Site"][$x]);
  $PI_ADDRESS = mysql_real_escape_string($_POST["Unit"][$x]);

  $qry="INSERT INTO test1 (AnalysisID, Elem, Lube) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
  mysql_query($qry);

 }

}

The problem is it doesn't matter what rows I check it always inserts the first in the list, it inserts the correct number just from the start of the list.

For example if I check the last 4 in the list I get the first 3.

Any pointers would be a great help.

jtheman
  • 7,421
  • 3
  • 28
  • 39

1 Answers1

0

Checkbox values are only sent if the checkboxes are checked, so the index will be different in the $_POST["name"] and the other $_POST arrays. (Unchecked checkbox inputs are seen as "unsuccessful" https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.2 )

Try adding the ID as index instead, then selected data will keep together. (I renamed the name array to aid to better match the contents)

<td>
     <input type="checkbox" name="aid[<?php echo $row['AnalysisID']; ?>]" value="1">
     <?php echo $row["CustomerName"]; ?>
     <input type="hidden" name="Site[<?php echo $row['AnalysisID']; ?>]" value="<?php echo $row["Site"]; ?>">
     <input type="hidden" name="Unit[<?php echo $row['AnalysisID']; ?>]" value="<?php echo $row["Unit"]; ?>">
</td>

Then looping through the post data with foreach(). Only the checked ID's will be posted so no need for if (!empty() check.

if (isset($_POST["aid"])) { // to avoid php undefined notice if none selected
  foreach ($_POST["aid"] as $id=>$value){ // the index is what we need, using $id

    $PI_NO = mysql_real_escape_string($id);
    $CUSTOMER_NAME = mysql_real_escape_string($_POST["Site"][$id]);
    $PI_ADDRESS = mysql_real_escape_string($_POST["Unit"][$id]);

    $qry="INSERT INTO test1 (AnalysisID, Elem, Lube) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
    mysql_query($qry);

  }
}

Also I strongly advice you to change from using deprecated mysql_* functions, use mysqli or PDO instead. Why shouldn't I use mysql_* functions in PHP?

jtheman
  • 7,421
  • 3
  • 28
  • 39
  • Thank you, yes i will look as mysqli as soon as i get this working, unfortunately that didn't work still only putting in the first in the list –  Mar 14 '18 at 09:11
  • More information, the ID number going over it correct, its the Unit and Site that it's taking the first number of. –  Mar 14 '18 at 09:32
  • 1
    Of course, it was making it out of step, thank you so much. –  Mar 14 '18 at 15:23
  • Quick update (and Question)... It will only insert up to row number 100, so if I check say the 99th one on the list and the 101st one only the 99th gets inserted, if I check 110th and 111th it hangs. Been trying things for days, any ideas? –  Apr 24 '18 at 11:55