943,670 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 8480
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 13th, 2008
1

help with delete multiple rows in mysql using checkboxes

Expand Post »
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:

php Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008
Aug 13th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

see this code will work at this url:
http://www.daniweb.com/forums/thread134908.html
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 13th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

it won't help sorry.. coz im lookin for the delete function not validation
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008
Aug 13th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

hello..
see that code is not for validating...
for checking checkboxes only...
its working fine for me...
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 13th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008
Aug 13th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

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)
php Syntax (Toggle Plain Text)
  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 C; Aug 13th, 2008 at 7:00 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 14th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008
Aug 14th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

thank you!!!! i will try the code. =)
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008
Aug 14th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

hi.. i am a also a newbie.. try this one.. this worked for me.
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 2
Light Poster
enim213 is offline Offline
40 posts
since Jun 2008
Aug 19th, 2008
0

Re: help with delete multiple rows in mysql using checkboxes

finally!!!!!!! it worked! thank you to everyone especially to enim!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
fortiz147 is offline Offline
18 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Update values
Next Thread in PHP Forum Timeline: Getting str_replace function on ' sign





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC