TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-11-2008, 10:01 AM   #1 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default Checkbox mass update?

Hi,

I've built an approval process for submitted messages with an approve or delete button next to each message. This links through to either:

url.php?approve=$id
url.php?delete=$id

But i want to change them from links to checkboxes so that i can update multiple records at once.

How would i go about this?

Thanks,
Mike
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 03-11-2008, 03:23 PM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Make your form dynamically and run through it with a forloop. Like;

Code:
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="checkbox" name="checkbox[]" value="id[]">
</form>
As you can see, both the value and the checkbox has a double bracket, which tells PHP that it's an array (same as the $_POST global).

So, if you want to mass update it, simply put in a forloop;
PHP Code:
for ($i 0$i count($_POST['checkbox']); $i++) {
mysql_query('UPDATE myTable SET myRow = "'.$_POST['checkbox'][$i].'" WHERE id = "'.$_POST['id'][$i].'" LIMIT 1');

I hope this helps, I haven't tried it but it should work.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 03-11-2008, 07:33 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Maybe it's just paranoia but I would clean the input first..
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-11-2008, 07:52 PM   #4 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Quote:
Originally Posted by TlcAndres View Post
Maybe it's just paranoia but I would clean the input first..
No it isn't just paranoia. Sanitisation is a must if you are to defy silly little kiddy hackers who think they are cool by trying to SQL Inject you :)
Gareth is offline  
Reply With Quote
Old 03-11-2008, 08:13 PM   #5 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default

Thanks for the advice guys, will make sure put a stop to that sql injection and will let you know if i have any problems with the rest of the script :)
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 03-12-2008, 09:27 AM   #6 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default

ok i'm running into a little problem...

I have set all of the check boxes to:
PHP Code:
<input type="checkbox" name="checkbox[]" value="id[]" /> 
and have this code:
PHP Code:
for ($i 0$i count($_POST['checkbox']); $i++) {            
 
//echo "myAction: $myAction<br />i: $i<br />";
 
mysql_query('UPDATE orders SET '.$myAction.' = "Y" WHERE id = "'.$_POST['id'][$i].'" LIMIT 1');

in that loop it's saying where id = i but i need i to be the id number of that record but it hasnt been set anywhere, should i do:
PHP Code:
<input type="checkbox" name="checkbox[]" value="id[<?php echo $id?>]" />
or something like that?

Thanks
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 03-12-2008, 09:49 AM   #7 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

The value property of each checkbox should be set to the actual id of each item, and not to an array (id[]).

HTML Code:
<input type="checkbox" name="checkbox[]" value="<?= $id ?>" />
That $id will probably come from your database as you build the list of items.


To process the form data you use $_POST['checkbox'][$i]:
PHP Code:
for ($i 0$i count($_POST['checkbox']); ++$i) {
  echo 
"item id: " $_POST['checkbox'][$i];

I tested it here, this is the right way to do it.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
The Following User Says Thank You to DeMo For This Useful Post:
oMIKEo (03-12-2008)
Old 03-12-2008, 10:08 AM   #8 (permalink)
The Contributor
 
oMIKEo's Avatar
 
Join Date: Jan 2008
Location: Leeds
Posts: 52
Thanks: 7
oMIKEo is on a distinguished road
Default

Perfect, got that working great now.

Thanks guys! :)
Send a message via MSN to oMIKEo
oMIKEo is offline  
Reply With Quote
Old 03-12-2008, 02:39 PM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Gareth View Post
No it isn't just paranoia. Sanitisation is a must if you are to defy silly little kiddy hackers who think they are cool by trying to SQL Inject you :)
Seriously, I am not dumb or something? Why should I write a complete, complex system to filter out his input as well, if he asked for an example on a whole different subject.

So yes, paranoia.

Hopefully it'll work out oMIKEo!

/edit
I just noticed in your mysql_query() that you set the value with "Y" and perhaps "N". I advise you to set the field to int(1) and put a 0 for no, and a 1 for yes. That way, the system would be more secure and you wouldn't have to mess with upper or lower case characters.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 03-12-2008, 03:22 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
I advise you to set the field to int(1) and put a 0 for no, and a 1 for yes. That way, the system would be more secure and you wouldn't have to mess with upper or lower case characters.
There is a BOOLEAN / BOOL data type which you can use, where zero is false and non-zero is true. Storing true/false values within an INT column is a complete waste of space since that type uses four bytes. BOOL (or even TINYINT) only requires one byte. Specifying INT(1) does not restrict the range of allowed values to one byte in length (0-255 unsigned).
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 02:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design