Current location: Hot Scripts Forums » Programming Languages » PHP » Checkboxes and SQL queries


Checkboxes and SQL queries

Reply
  #1 (permalink)  
Old 07-27-04, 03:08 PM
Mikehive Mikehive is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Question Checkboxes and SQL queries

I can't figure out the most effective way to do this. I'm trying to write a script that will delete users in bulk from a forum; the idea is that the user can check the boxes for each user to remove and then the script clears them all out of the database table.

At the moment each checkbox is given the user's ID as its 'name'. Is there a way to make a query out of this that simply deletes all the users that have been checked, without testing every user one-by-one to see if their value is 'on'? Or is there a better way to set up such a script? Please, any logistical or script advice would be very welcome.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 07-27-04, 03:16 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
PHP Code:

<?php


foreach ($_POST['delete'] as $user_id)
@
mysql_query("DELETE FROM users WHERE user_id = '" $user_id "'");

?>
That's assuming the checkboxes are named "delete"...

Oh yeah, this is how you'd build the checkboxes from the database. The "name" input would be the same, it's the values that would change from user to user:
Code:
<!--
Use the format:
<input type="checkbox" name="delete" value="USER_ID"> USER_NAME
--!>

<input type="checkbox" name="delete" value="1"> Hank
<input type="checkbox" name="delete" value="2"> Jim
<input type="checkbox" name="delete" value="4"> Bob
<input type="checkbox" name="delete" value="5"> Arnold
<input type="checkbox" name="delete" value="7"> Steve

Last edited by Keith; 07-27-04 at 03:25 PM.
Reply With Quote
  #3 (permalink)  
Old 07-27-04, 07:34 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
Keith's code is suppose to work, but it if doesn't try putting a [] after the delete so it will be name="delete[]" and if that doesn't work try putting a unquie index for each one in there.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #4 (permalink)  
Old 07-27-04, 07:44 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Ohhh yeah yeah yeah... I totally forgot about that. Oops oops oops...

Code:
<input type="checkbox" name="delete[]" value="1"> Hank
etc...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
asp: validating checkboxes in groups by groupid seala ASP 0 09-08-03 01:36 PM
asp: checkboxes & multi-page form seala ASP 0 09-02-03 01:58 PM
Dynamic form checkboxes (ASP) Claralu ASP 3 08-06-03 03:37 PM


All times are GMT -5. The time now is 04:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.