help with delete multiple rows in mysql using checkboxes

Thread Solved

Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

help with delete multiple rows in mysql using checkboxes

 
1
  #1
Aug 13th, 2008
please help i don't know how to delete multiple rows in mysql using checkboxes, because it seems the delete doesn't seem to work.
here's the code:

  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 fortiz147; Aug 13th, 2008 at 5:09 am. Reason: code tag
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #2
Aug 13th, 2008
see this code will work at this url:
http://www.daniweb.com/forums/thread134908.html
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #3
Aug 13th, 2008
it won't help sorry.. coz im lookin for the delete function not validation
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #4
Aug 13th, 2008
hello..
see that code is not for validating...
for checking checkboxes only...
its working fine for me...
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #5
Aug 13th, 2008
yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #6
Aug 13th, 2008
ya,this is the continuation code for the above post...
after getting all check boxes you checked,then write this query to delete them(checked)
  1. if(isset($_POST['check_compare']))
  2. {
  3. $tempids=$_POST['check_compare'];
  4. echo $tempids;
  5.  
  6. $ser_qry="delete from sometable where find_in_set(p_id,'".$_POST['check_compare']."')";
  7. $ser_res=mysql_query($ser_qry);
  8.  
  9.  
  10. }
Last edited by Shanti Chepuru; Aug 13th, 2008 at 7:00 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #7
Aug 14th, 2008
yep but i need the delete function for mysql >.< i dont know how to query the data from mysql in php when using the checkboxes in each row.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #8
Aug 14th, 2008
thank you!!!! i will try the code. =)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 40
Reputation: enim213 is an unknown quantity at this point 
Solved Threads: 2
enim213 enim213 is offline Offline
Light Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #9
Aug 14th, 2008
hi.. i am a also a newbie.. try this one.. this worked for me.
  1. <html>
  2. <head>
  3.  
  4. </head>
  5. <body>
  6. <?php
  7. //mysql connection
  8. $dbconn = mysql_connect("","","") or die();
  9. mysql_select_db("table") or die();
  10.  
  11. $sqlquery = "your query"; // query on table
  12. $sqlresult = mysql_query($sqlquery, $dbconn);
  13. $count = mysql_num_rows($sqlresult); // count query result
  14.  
  15. ?>
  16.  
  17. <form method="post" action="delete.php">
  18. <!--
  19. some table headers here / or html tags
  20. -->
  21. <?php
  22. //loop here
  23. while($row = mysql_fetch_array($sqlresult))
  24. {
  25. //inside while
  26. ?>
  27. <tr>
  28. <td><input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $row['id']?>" /></td>
  29. <!--
  30. and other data you want to print out..
  31. -->
  32. </tr>
  33. <?php
  34. }
  35. ?>
  36. <tr><input id='delete' type='submit' name='delete' value='Delete'/></tr>
  37. </form>
  38. </body>
  39. </html>
  40.  
  41. <?php
  42. #these following code for delete.php file
  43. ?>
  44.  
  45. <html>
  46. <?php
  47. //mysql connection here
  48.  
  49. if($_POST['delete']) // from button name="delete"
  50. {
  51. $checkbox = $_POST['checkbox']; //from name="checkbox[]"
  52. $countCheck = count($_POST['checkbox']);
  53.  
  54. for($i=0;$i<$countCheck;$i++)
  55. {
  56. $del_id = $checkbox[$i];
  57. $sql = "delete from table where id = $del_id";
  58. $result = mysql_query($sql, $dbconn);
  59. }
  60. if($result)
  61. {
  62. echo "successful delete";
  63. }
  64. else
  65. {
  66. echo "Error: ".mysql_error();
  67. }
  68. }
  69. ?>
  70. </html>
Last edited by enim213; Aug 14th, 2008 at 5:21 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 18
Reputation: fortiz147 is an unknown quantity at this point 
Solved Threads: 0
fortiz147 fortiz147 is offline Offline
Newbie Poster

Re: help with delete multiple rows in mysql using checkboxes

 
0
  #10
Aug 19th, 2008
finally!!!!!!! it worked! thank you to everyone especially to enim!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC