0

I am trying to insert multiple rows to a database table if check box is selected. But in my code when I am trying to insert, new rows are inserting based on check box selection. But no data is passing. I need some advice on below code to modify:

<?php
  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);
  $qry="select * from  pi";
  $result=mysql_query($qry);
?>

<form action="check.php"   method="post">
  <table>
    <tr>
      <th>A</th>
      <th>B</th>
      <th>C</th>
   </tr>

<?php
while($row=mysql_fetch_array($result))
{
  echo "<tr><td><input type=checkbox name=name[] value='".$row['id']."'>".$row['PI_NO']."</td><td>".$row['CUSTOMER_NAME']."</td><td>".$row['PI_ADDRESS']."</td></tr>";
}
?>

<input type="submit" value="save" id="submit">

<?php
  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);

  $name=$_POST['name'];
  foreach($_POST['name'] as $x)
  {
    $qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
    mysql_query($qry);
  }     
?>
jh314
  • 27,144
  • 16
  • 62
  • 82
Ripon
  • 81
  • 2
  • 8

2 Answers2

1

Notes:

  • You forgot to bind the name of your checkbox using a single tick (')
  • You used variables in your query which you didn't defined and assigned value with yet
  • You only passed on the value of name, and did not include the Pi Address and Customer name. I'll be passing them by hidden input using <input type="hidden">.
  • I'll change the way you check your passed on form by looping them and check them using for() and if()
  • Use mysql_real_escape_string() before using them in your queries to prevent some of the SQL injections. But better if you consider using mysqli prepared statement rather than the deprecated mysql_*.
  • Is your post a single file? If it is, you must enclose your query using an isset() to prevent error upon loading the page.
  • You didn't close your <form>

Here's your corrected while loop:

<?php
while($row=mysql_fetch_array($result))
{
  ?>
    <tr>
      <td>
        <input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>">
        <?php echo $row["PI_NO"]; ?>
        <!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT -->
        <input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>">
        <input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>">
      </td>
      <td><?php echo $row['CUSTOMER_NAME']; ?></td>
      <td><?php echo $row['PI_ADDRESS']; ?></td>
    </tr>
  <?php
} /* END OF WHILE LOOP */
?>

<input type="submit" value="save" id="submit">
</form> <!-- YOU DID NOT CLOSE YOUR FORM IN YOUR POST -->

And your query:

<?php

  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);

  $counter = count($_POST["name"]); /* COUNT THE PASSED ON 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["customer"][$x]);
      $PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);

      $qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
      mysql_query($qry);

    } /* END OF CHECKING THE CHECKBOX IF SELECTED */

  } /* END OF FOR LOOP */

?>
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • Thanks to all for your valuable advice. I have corrected the problem and understood the error in my coding. is there any way i can display value in text box, change and then send it to table to add. – Ripon May 12 '15 at 05:41
  • I have able to do text box insert into database. thanks to all. – Ripon May 12 '15 at 05:54
  • I am unable to save the rows i am selecting. it is taking rows 1,2,3..... from the table when save. – Ripon May 12 '15 at 10:55
0

Lots of little problems. And some big ones.

as $x){ .. $x is not being used so I assume you just loop for the number of checked boxes.

These have no values: '$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS'

Missing </form>

Not being used: $name=$_POST['name'];

<?php
echo '<form action="check.php"   method="post"><table><tr><th>A</th><th>B</th><th>C</th></tr>';

$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$sql = "select `id`,`PI_NO`, `CUSTOMER_NAME` ,`PI_ADDRESS` from  `pi`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
  echo "<tr><td><input type=\"checkbox\" name=\"name[]\" value=/"$row[0]/"'>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}

echo '<input type="submit" value="save" id="submit"></form>';
foreach($_POST['name'] as $x){
  $sql="INSERT INTO pi (`PI_NO`, `CUSTOMER_NAME`, `PI_ADDRESS`)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
  mysql_query($sql);
}
?>
Misunderstood
  • 5,534
  • 1
  • 18
  • 25