943,608 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 29323
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 15th, 2007
0

MySQL Checkbox Multiple data Delete?

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

php Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NeoNe is offline Offline
1 posts
since May 2007
May 16th, 2007
0

Re: MySQL Checkbox Multiple data Delete?

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')
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hookedonphp is offline Offline
4 posts
since Nov 2006
May 16th, 2007
0

Re: MySQL Checkbox Multiple data Delete?

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
Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
May 16th, 2007
0

Re: MySQL Checkbox Multiple data Delete?

Reputation Points: 18
Solved Threads: 9
Junior Poster
w_3rabi is offline Offline
160 posts
since Dec 2006
May 22nd, 2007
0

Re: MySQL Checkbox Multiple data Delete?

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
Reputation Points: 10
Solved Threads: 0
Light Poster
php_coder is offline Offline
34 posts
since Dec 2006
Jun 20th, 2007
0

Re: MySQL Checkbox Multiple data Delete?

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!!!
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
it2051229 is offline Offline
82 posts
since May 2007
Nov 21st, 2008
0

Re: MySQL Checkbox Multiple data Delete?

I m facing the same prob buddy if u got solved then plz share it with me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sajjadpk is offline Offline
2 posts
since Nov 2008
Nov 21st, 2008
0

Re: MySQL Checkbox Multiple data Delete?

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.
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
it2051229 is offline Offline
82 posts
since May 2007
Dec 9th, 2008
0

Re: MySQL Checkbox Multiple data Delete?

For The Check All/Uncheck All Functions.
Just Change The Name Of The Form In The Javascript And The HTML As Needed

The Javascript:
Javascript Syntax (Toggle Plain Text)
  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:
HTML Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Logan_Yupp is offline Offline
3 posts
since Dec 2008
Dec 9th, 2008
0

Re: MySQL Checkbox Multiple data Delete?

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:
HTML Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Logan_Yupp is offline Offline
3 posts
since Dec 2008

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: php code to send sms messages
Next Thread in PHP Forum Timeline: Warning: include(/view/login.php) [function.include]: failed to open stream:





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


Follow us on Twitter


© 2011 DaniWeb® LLC