Delete multiple rows in mysql with check box

Reply

Join Date: May 2007
Posts: 20
Reputation: pranto157 is an unknown quantity at this point 
Solved Threads: 0
pranto157 pranto157 is offline Offline
Newbie Poster

Delete multiple rows in mysql with check box

 
0
  #1
Dec 19th, 2007
hi all. I have got below code from a website. Its work all but its can't delete the selected multiple row. plz help me... where is the problem.
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="root"; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="forum"; // 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 test_mysql";
  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, MYSQL_ASSOC)){
  35.  
  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"><?php echo $rows['id']; ?></td>
  40. <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
  41. <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
  42. <td bgcolor="#FFFFFF"><?php 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. <?php
  51. // Check if delete button active, start this
  52. if($delete){
  53.  
  54. for($i=0;$i<5;$i++){
  55.  
  56. $del_id = $checkbox[$i];
  57.  
  58. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  59. $result = mysql_query($sql);
  60. }
  61.  
  62. // if successful redirect to delete_multiple.php
  63. if($result){
  64. echo "<meta http-equiv=\"refresh\" content=\"0;URL=dele.php\">";
  65. }
  66. }
  67. mysql_close();
  68. ?>
  69. </table>
  70. </form>
  71. </td>
  72. </tr>
  73. </table>
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 20
Reputation: pranto157 is an unknown quantity at this point 
Solved Threads: 0
pranto157 pranto157 is offline Offline
Newbie Poster

Re: Delete multiple rows in mysql with check box

 
0
  #2
Dec 19th, 2007
here is the Database--

CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
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 mysql with check box

 
0
  #3
Dec 19th, 2007
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="root"; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="forum"; // 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 test_mysql";
  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, MYSQL_ASSOC)){
  35.  
  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"><?php echo $rows['id']; ?></td>
  40. <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
  41. <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
  42. <td bgcolor="#FFFFFF"><?php 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. <?php
  51. // Check if delete button active, start this
  52. if($_POST['delete']){
  53. //print_r($_POST);
  54. //exit;
  55. for($i=0;$i<count($_POST['checkbox']);$i++){
  56.  
  57. $del_id = $checkbox[$i];
  58.  
  59. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  60. $result = mysql_query($sql);
  61. }
  62.  
  63. // if successful redirect to delete_multiple.php
  64. if($result){
  65. echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";
  66. }
  67. }
  68. mysql_close();
  69. ?>
  70. </table>
  71. </form>
  72. </td>
  73. </tr>
  74. </table>

This should work.

Cheers,
Nav
Ignorance is definitely not bliss!

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

Re: Delete multiple rows in mysql with check box

 
0
  #4
Dec 19th, 2007
sorry . this code does not work...
i think the value of checkbox doesnot passing...
there is the problem...
i think the checkbox value is null
i try this code by giving
DELETE FROM $tbl_name WHERE id=1
and its work
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
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 mysql with check box

 
0
  #5
Dec 19th, 2007
It works for me though! But anyway, try the following.
  1. $checkbox=$_POST['checkbox'];
  2. for($i=0;$i<count($checkbox);$i++){
  3. $del_id = $checkbox[$i];
  4. .......
Ignorance is definitely not bliss!

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

Re: Delete multiple rows in mysql with check box

 
0
  #6
Dec 19th, 2007
no its does not work
$del_id = $checkbox[$i];
i try to print print or echo $del_id
but it does not returns any value....
plz help nav33n
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
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 mysql with check box

 
0
  #7
Dec 19th, 2007
hmm.. can you show your latest script ?
Ignorance is definitely not bliss!

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

Re: Delete multiple rows in mysql with check box

 
0
  #8
Dec 19th, 2007
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum";
$tbl_name="test_mysql";
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$sql="SELECT * FROM test_mysql";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr><td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td></tr>
<tr><td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td></tr>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>

<tr><td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>">
</td><td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td><td bgcolor="#FFFFFF">
<?php echo $rows['name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?>
</td></tr>
<?php } ?>
<tr><td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td></tr>
<?php
if($_POST['delete']){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);}

if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}}
mysql_close();?></table></form></td></tr></table>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
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 mysql with check box

 
0
  #9
Dec 19th, 2007
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="root"; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="test";
  6. $tbl_name="users";
  7. mysql_connect($host, $username, $password)or die("cannot connect");
  8. mysql_select_db($db_name)or die("cannot select DB");
  9. $sql="SELECT * FROM $tbl_name";
  10. $result=mysql_query($sql);
  11. $count=mysql_num_rows($result);
  12. ?>
  13. <table width="400" border="0" cellspacing="1" cellpadding="0">
  14. <tr><td><form name="form1" method="post" action="">
  15. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"><tr>
  16. <td bgcolor="#FFFFFF">&nbsp;</td>
  17. <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td></tr>
  18. <tr><td align="center" bgcolor="#FFFFFF">#</td>
  19. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  20. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  21. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  22. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td></tr>
  23. <?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
  24.  
  25. <tr><td align="center" bgcolor="#FFFFFF">
  26. <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['user_id']; ?>">
  27. </td><td bgcolor="#FFFFFF"><?php echo $rows['user_id']; ?></td><td bgcolor="#FFFFFF">
  28. <?php echo $rows['login_name']; ?></td><td bgcolor="#FFFFFF"><?php echo $rows['first_name']; ?></td>
  29. <td bgcolor="#FFFFFF"><?php echo $rows['email']; ?>
  30. </td></tr>
  31. <?php } ?>
  32. <tr><td colspan="5" align="center" bgcolor="#FFFFFF">
  33. <input name="delete" type="submit" id="delete" value="Delete"></td></tr>
  34. <?php
  35. if($_POST['delete']){
  36. for($i=0;$i<count($_POST['checkbox']);$i++){
  37. $del_id = $checkbox[$i];
  38. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  39. print $sql;
  40. $result = mysql_query($sql);}
  41.  
  42. if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";}}
  43. mysql_close();
  44. }
  45. ?>
  46. </table></form></td></tr></table>

This works for me. Umm.. I have also printed the query and it prints , DELETE FROM users WHERE id='1'
DELETE FROM users WHERE id='2'
DELETE FROM users WHERE id='3'

I have no clue why your code is 'losing' checkbox array values.


Try taking out this part if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";} and then check. Maybe the page is getting refreshed, so its losing the posted values. Lets see if that works.

Cheers,
Nav
Last edited by nav33n; Dec 19th, 2007 at 10:20 am.
Ignorance is definitely not bliss!

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

Re: Delete multiple rows in mysql with check box

 
0
  #10
Dec 20th, 2007
i use php ----
Apache version :Apache/2.0.55 (Win32) PHP version :5.1.2 Loaded extensions :
bcmath, calendar, com_dotnet, ctype, date, ftp, iconv, odbc, pcre, Reflection, session, libxml, standard, tokenizer, zlib, SimpleXML, dom, SPL, wddx, xml, xmlreader, xmlwriter, apache2handler, mbstring, mysql, mysqli, PDO, pdo_sqlite, SQLite MySQL version :5.0.18-nt

this the problem... i want to say is the problem of php version....
or do i need any change in my php.ini file
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC