php - Multiple row insert to table if check box is selected -


i trying insert multiple rows database table if check box selected. in code when trying insert, new rows inserting based on check box selection. no data passing. need advice on below code modify:

<?php   $db=mysql_connect("localhost","root","");   mysql_select_db("kkk",$db);   $qry="select *  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'] $x)   {     $qry="insert pi (pi_no, customer_name, pi_address)values ('$pi_no','$customer_name','$pi_address')";     mysql_query($qry);   }      ?> 

notes:

  • you forgot bind name of checkbox using single tick (')
  • you used variables in query didn't defined , assigned value yet
  • you passed on value of name, , did not include pi address , customer name. i'll passing them hidden input using <input type="hidden">.
  • i'll change way check passed on form looping them , check them using for() , if()
  • use mysql_real_escape_string() before using them in queries prevent of sql injections. better if consider using mysqli prepared statement rather deprecated mysql_*.
  • is post single file? if is, must enclose query using isset() prevent error upon loading page.
  • you didn't close <form>

here's 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 start of 2 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> <!-- did not close form in post --> 

and query:

<?php    $db=mysql_connect("localhost","root","");   mysql_select_db("kkk",$db);    $counter = count($_post["name"]); /* count 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 pi (pi_no, customer_name, pi_address) values ('$pi_no','$customer_name','$pi_address')";       mysql_query($qry);      } /* end of checking checkbox if selected */    } /* end of loop */  ?> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -