MySQL Checkbox Multiple data Delete?

Reply

Join Date: May 2007
Posts: 1
Reputation: NeoNe is an unknown quantity at this point 
Solved Threads: 0
NeoNe's Avatar
NeoNe NeoNe is offline Offline
Newbie Poster

MySQL Checkbox Multiple data Delete?

 
0
  #1
May 15th, 2007
Hi to everyone I’m wondering how to make some simple checkbox for multiple data delete from mysql I found some tutorial on net but don’t work. I tested it without changing anything and doesn’t work. I’m wondering did programmer test it before he posted that tutorial ?

Here is a link of that tutorial if u don’t believe me test it.
phpeasystep.com

I’m working on some cms tool to make posts on site I used a lot of cms like Joomla, WordPress and Textpattern. Now I want to make something like that, of course not even similar to that but just for my personal use and fun.

2 things to do:
  1. Option to select all checkboxes. I like something like in joomla in top there is checkbox with titles when u select it all checkbox are selected and if u uncheck it all is unchecked
  2. When I press delete button all checkboxes that I select are deleted from database or moved to some other category like trash which is nice because u can in that way return some posts that u delete by mistake or some other reason
Here is my code that bothers me becouse checkboxes don't work

  1. <?php
  2. include ('conf/config.php');
  3. include('conf/opendb.php');
  4. $sql="SELECT id, title, author, DATE_FORMAT(date, '%M %d, %Y') as sd FROM $tbl_name";
  5. $result=mysql_query($sql);
  6.  
  7. $count=mysql_num_rows($result);
  8.  
  9. ?>
  10. <table border="0" cellspacing="0" cellpadding="0">
  11. <tr>
  12. <td><form name="form1" method="post" action="">
  13. <table border="0" cellpadding="0" cellspacing="0" class="adminlist">
  14. <tr id="top">
  15. <th width="5" align="center">Id</th>
  16. <th width="5" align="center">#</th>
  17. <th>Title</th>
  18. <th width="200" align="center">Author</th>
  19. <th width="100" align="center">Date</th>
  20. <th width="80" align="center">Delete</th>
  21. <th width="20" align="center">Edit</th>
  22. </tr>
  23. <?php
  24. while($rows = mysql_fetch_array($result)){
  25. ?>
  26. <tr>
  27. <td width="5" align="center"><? echo $rows['id']; ?></td>
  28. <td width="5" align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
  29. <td><? echo $rows['title']; ?></td>
  30. <td width="200" align="center"><? echo $rows['author']; ?></td>
  31. <td width="100" align="center"><? echo $rows['sd']; ?></td>
  32. <td width="80" align="center" id="trash"><a href="delete_news.php?id=<? echo $rows['id']; ?>">Delete</a></td>
  33. <td width="20" align="center" id="edit"><a href="edit_news.php?id=<? echo $rows['id']; ?>">Edit</a></td>
  34. <?php
  35. }
  36. ?>
  37. <tr>
  38. <td colspan="7" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td>
  39. </tr>
  40. <?
  41. // Check if delete button active, start this
  42. if($delete){
  43. for($i=0;$i<$count;$i++){
  44. $del_id = $checkbox[$i];
  45. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  46. $result = mysql_query($sql);
  47. }
  48.  
  49. // if successful redirect to delete_multiple.php
  50. if($result){
  51. echo "<meta http-equiv=\"refresh\" content=\"0;URL=post_manage.php\">";
  52. }
  53. }
  54. mysql_close();
  55. ?>
  56. </table>
  57. </form>
  58. </td>
  59. </tr>
  60. </table>

what could be wrong?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: hookedonphp is an unknown quantity at this point 
Solved Threads: 0
hookedonphp hookedonphp is offline Offline
Newbie Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #2
May 16th, 2007
The author may have register_globals ON, while you have (appropriately) turned it OFF.

Evaluate $_POST['delete'] to see if the value really is "Delete". Instead of simply
if($delete), then if($_POST['delete'] == 'Delete')
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #3
May 16th, 2007
about the CMS there is another guy on daniweb who is making a CMS for fun
u could work with him if u like so u could come out with something useful
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #4
May 16th, 2007
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 34
Reputation: php_coder is an unknown quantity at this point 
Solved Threads: 0
php_coder php_coder is offline Offline
Light Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #5
May 22nd, 2007
HIii

while checking if delete button is active or not
you should say

if($_REQUEST['delete']))

{
//code here//
}

Hope now it will work. let me knw after u try...
Cheers
Prati
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 81
Reputation: it2051229 is an unknown quantity at this point 
Solved Threads: 1
it2051229 it2051229 is offline Offline
Junior Poster in Training

Re: MySQL Checkbox Multiple data Delete?

 
0
  #6
Jun 20th, 2007
Well i'm having a problem with the compatibility of javascript and PHP multiple delete check box.. i used a javascript for the "CHECK ALL BOXES" just like yahoo mail.. so my input is something like this "<input type='checkbox' name='checkbox' value='1'><input type='checkbox' name='checkbox' value='2'>" and the check all boxes function worked but it doesnt work with my PHP code where it is something like this :

$checked = $_POST["checkbox"];
for($i=0; $i < count($checked); $i++) {
....// DELETE ALL CHECKED
}

but if i change my input to something like: <input type='checkbox' name='checkbox[]' value='1'><input type='checkbox' name='checkbox[]' value='2'>.. it does work on my PHP script but it wont work for my "Check all Function" javascript.. damn it.. i need help!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: sajjadpk is an unknown quantity at this point 
Solved Threads: 0
sajjadpk sajjadpk is offline Offline
Newbie Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #7
Nov 21st, 2008
I m facing the same prob buddy if u got solved then plz share it with me
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 81
Reputation: it2051229 is an unknown quantity at this point 
Solved Threads: 1
it2051229 it2051229 is offline Offline
Junior Poster in Training

Re: MySQL Checkbox Multiple data Delete?

 
0
  #8
Nov 21st, 2008
Yes this problem of mine was since i was still learning javascript. What i did is the name is still the same where the name has the '[]' brackets so that it will work with PHP but the javascript will be different. If you mind posting your javascript so that i can see.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Logan_Yupp is an unknown quantity at this point 
Solved Threads: 0
Logan_Yupp Logan_Yupp is offline Offline
Newbie Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #9
Dec 9th, 2008
For The Check All/Uncheck All Functions.
Just Change The Name Of The Form In The Javascript And The HTML As Needed

The Javascript:
  1. function CheckAll() {
  2. count = document.deleteform.elements.length;
  3. for (i=0; i < count; i++) {
  4. if (document.deleteform.elements[i].checked == 0) {
  5. document.deleteform.elements[i].checked = 1;
  6. } else {
  7. document.deleteform.elements[i].checked = 1;
  8. }
  9. }
  10. }
  11.  
  12. function UncheckAll() {
  13. count = document.deleteform.elements.length;
  14. for (i=0; i < count; i++) {
  15. if (document.deleteform.elements[i].checked == 1) {
  16. document.deleteform.elements[i].checked = 0;
  17. } else {
  18. document.deleteform.elements[i].checked = 0;
  19. }
  20. }
  21. }
The HTML:
  1. <form name="deleteform" action="somepage" method="post">
  2.  
  3. <input type="checkbox" name="ANY NAME" value="THE VALUE" />
  4.  
  5. <a href="javascript:void()" onClick = "CheckAll()">Select All</a>
  6. <a href="javascript:void()" onClick = "UncheckAll()">Select None</a>
  7.  
  8. </form>
Last edited by Logan_Yupp; Dec 9th, 2008 at 7:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Logan_Yupp is an unknown quantity at this point 
Solved Threads: 0
Logan_Yupp Logan_Yupp is offline Offline
Newbie Poster

Re: MySQL Checkbox Multiple data Delete?

 
0
  #10
Dec 9th, 2008
I've been experimenting with my php code for a while now and i found a way to perform the task of deleting the check-boxed items:

In the html, make sure all the check-boxed items have are named with brackets '[]' after the name, Like:
  1. <input type="checkbox" name="delete[]" value="THE VALUE" />

Post the results to the second page. In the second page, assign the posted results to a variable. In my example, the variable is $delete:
  1. $delete = $_POST['delete'];
  2.  
  3.  
  4. //Then do what you want with the selected items://
  5.  
  6. foreach ($delete as $id) {
  7.  
  8. $q = "DELETE FROM table_name WHERE item_id = $id LIMIT 1"; //THE QUERY
  9. $r = @mysqli_query($DATABASE_CONNECTION, $q); //EXECUTE QUERY
  10. }
  11.  
  12.  
  13. //Show that the items have been successfully removed.//
  14.  
  15. if (mysqli_affected_rows($DATABASE_CONNECTION) > 0) {
  16. echo '<p>The selected items have been successfully deleted.</p>';
  17. } else {
  18. echo '<p>An error has occurred while processing your request</p>';
  19. }
Reply With Quote Quick reply to this message  
Reply

Message:



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



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

©2003 - 2009 DaniWeb® LLC