|
-
Aug 29th, 2005, 01:13 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Mysql Help!!!!!!
okay in my script i have it list a bunch of checkbox's.
The code is sumtin like this for listing the checkbox's with each extracted name.
PHP Code:
$sql = "SELECT * FROM members WHERE trainer = 1";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo" <tr>
<td scope=col><input name=rem_trainer type=checkbox value=$row[username]>$row[username]</td>
</tr>";
}
now when i check multiple box's and hit remove trainer. I want to remove each of the selected.
This is how i have it so far... tho it only works for 1 at a time.
PHP Code:
if(isset($_POST['removed'])){
echo "Removed trainer " . $_POST['rem_trainer'] ." from list.<BR>";
mysql_query("UPDATE members SET trainer = 0 WHERE username = '$_POST[rem_trainer]'") or die(mysql_error());
}
any help would be greatly apperciated.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
Aug 29th, 2005, 01:57 AM
#2
Re: Mysql Help!!!!!!
You need to append square brackets to the checkbox names. This will cause PHP to create an array when the data is posted. You can then loop through the array to execute the query for each selected checkbox:
HTML Code:
<input name="rem_trainer[]" type="checkbox" />
PHP Code:
if (is_array($_POST['rem_trainer'])) {
foreach($_POST['rem_trainer'] as $trainer) {
$trainer = addslashes($trainer); // use if magic quotes is disabled
mysql_query("UPDATE members SET trainer = 0 WHERE username = '$trainer'") or die(mysql_error());
echo "Removed trainer " . $_POST['rem_trainer'] ." from list.<BR>";
}
}
P.s: A little coding tip. It improves readability if you embed the PHP inside the HTML. Like this:
PHP Code:
<?php
$sql = "SELECT * FROM members WHERE trainer = 1";
$result = mysql_query($sql) or die(mysql_error());
?>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td scope=col><input name="rem_trainer[]" type="checkbox"
value="<?php echo($row[username]) ?>" /><?php echo($row[username]) ?></td>
</tr>
<?php endwhile; ?>
Check the alternate template like while syntax. PHP also offers the same for if, for, foreach and switch. Have a look here: http://uk.php.net/manual/en/control-...ive-syntax.php
-
Aug 29th, 2005, 02:40 AM
#3
Thread Starter
Hyperactive Member
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|