Delete Multiple Rows in PHP

Thread Solved

Join Date: Apr 2009
Posts: 3
Reputation: xila is an unknown quantity at this point 
Solved Threads: 0
xila xila is offline Offline
Newbie Poster

Delete Multiple Rows in PHP

 
0
  #1
Apr 1st, 2009
I have been trying on this code for so long but I can't seem to find out why the rows could not be displayed / could not be fetched. The checkboxes are present but no info

Please help! (


  1. <?php
  2. $host="localhost"; // Host name
  3. $username=""; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="test"; // Database name
  6. $tbl_name="test_mysql"; // Table name
  7.  
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. $sql="SELECT * FROM $tbl_name";
  13. $result=mysql_query($sql);
  14.  
  15. $count=mysql_num_rows($result);
  16.  
  17. ?>
  18. <table width="400" border="0" cellspacing="1" cellpadding="0">
  19. <tr>
  20. <td><form name="form1" method="post" action="">
  21. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  22. <tr>
  23. <td bgcolor="#FFFFFF">&nbsp;</td>
  24. <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
  25. </tr>
  26. <tr>
  27. <td align="center" bgcolor="#FFFFFF">#</td>
  28. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  29. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  30. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  31. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  32. </tr>
  33. <?php
  34. while($rows=mysql_fetch_array($result)){
  35. ?>
  36. <tr>
  37. <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
  38. <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
  39. <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
  40. <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
  41. <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. <tr>
  47. <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
  48. </tr>
  49. <?
  50. // Check if delete button active, start this
  51. if($delete){
  52. for($i=0;$i<$count;$i++){
  53. $del_id = $checkbox[$i];
  54. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  55. $result = mysql_query($sql);
  56. }
  57.  
  58. // if successful redirect to delete_multiple.php
  59. if($result){
  60. echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
  61. }
  62. }
  63. mysql_close();
  64. ?>
  65. </table>
  66. </form>
  67. </td>
  68. </tr>
  69. </table>
Last edited by peter_budo; Apr 1st, 2009 at 4:48 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Delete Multiple Rows in PHP

 
0
  #2
Apr 1st, 2009
Could be many reasons.
1. php short tags may be disabled. Use <?php
2. Register globals may be disabled. Use $_POST['form_element'] instead of $form_element.
This is an example.
  1. <?php
  2. if(isset($_POST['submit'])) {
  3. foreach($_POST['checkbox'] as $checked) {
  4. //delete checked record
  5. }
  6. }
  7. ?>
  8. <html>
  9. <body>
  10. <form method='post'>
  11. <input type='checkbox' name='checkbox[]' value='1' /> 1 <br />
  12. <input type='checkbox' name='checkbox[]' value='2' /> 2 <br />
  13. <input type='checkbox' name='checkbox[]' value='3' /> 3 <br />
  14. <input type='checkbox' name='checkbox[]' value='4' /> 4 <br />
  15. <input type='submit' name='submit' value='submit' />
  16. </form>
  17. </body>
  18. </html>
Welcome to Daniweb BTW. Please read the guidelines on how to use [ code ] tags for better readability of your code.
Last edited by nav33n; Apr 1st, 2009 at 3:33 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: xila is an unknown quantity at this point 
Solved Threads: 0
xila xila is offline Offline
Newbie Poster

Re: Delete Multiple Rows in PHP

 
0
  #3
Apr 1st, 2009
whoops! sorry about disobeying the code posting rules...but thanks!

  1.  
  2. <?php
  3. $host="localhost"; // Host name
  4. $username=""; // Mysql username
  5. $password=""; // Mysql password
  6. $db_name="test"; // Database name
  7. $tbl_name="test_mysql"; // Table name
  8.  
  9. // Connect to server and select databse.
  10. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  11. mysql_select_db("$db_name")or die("cannot select DB");
  12.  
  13. $sql="SELECT * FROM $tbl_name";
  14. $result=mysql_query($sql);
  15.  
  16. $count=mysql_num_rows($result);
  17.  
  18. ?>
  19. <table width="400" border="0" cellspacing="1" cellpadding="0">
  20. <tr>
  21. <td><form name="form1" method="post" action="">
  22. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  23. <tr>
  24. <td bgcolor="#FFFFFF">&nbsp;</td>
  25. <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
  26. </tr>
  27. <tr>
  28. <td align="center" bgcolor="#FFFFFF">#</td>
  29. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  30. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  31. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  32. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  33. </tr>
  34. <?php
  35. while($rows=mysql_fetch_array($result)){
  36. ?>
  37. <tr>
  38. <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
  39. <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
  40. <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
  41. <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
  42. <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. <tr>
  48. <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
  49. </tr>
  50. <?
  51. // Check if delete button active, start this
  52. if($delete){
  53. for($i=0;$i<$count;$i++){
  54. $del_id = $checkbox[$i];
  55. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  56. $result = mysql_query($sql);
  57. }
  58.  
  59. // if successful redirect to delete_multiple.php
  60. if($result){
  61. echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
  62. }
  63. }
  64. mysql_close();
  65. ?>
  66. </table>
  67. </form>
  68. </td>
  69. </tr>
  70. </table>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Delete Multiple Rows in PHP

 
0
  #4
Apr 1st, 2009
So, what is the problem ? Did you check if register globals are on in php.ini ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: xila is an unknown quantity at this point 
Solved Threads: 0
xila xila is offline Offline
Newbie Poster

Re: Delete Multiple Rows in PHP

 
0
  #5
Apr 2nd, 2009
thanks so much! now the rows are being displayed but the delete button is still not working, need to fix that too
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,748
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Delete Multiple Rows in PHP

 
0
  #6
Apr 2nd, 2009
Instead of calling the names directly, like $delete, try $_POST['delete']. ie., instead of
  1. if($delete){
Try,
  1. if(isset($_POST['delete'])) {
If that doesn't work, post your updated code.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC