Hi there, all ! I need your help My problem is: I have data in my mysql db (users) So I query it using while loop and they are one user per line with checkbox I want to if I check some of checkboxes so according username after submit will be deleted from database. Please help me, I need it - your help
Here's some code I've just copied from one of my scripts that selectively deletes messages from an inbox system I made... $msgcount=0; if ($_POST['delsel']) { $fetch=mysql_query("SELECT * FROM inbox WHERE `userid`='$uid' ORDER BY isread ASC, whenin DESC"); while ($inbox=mysql_fetch_array($fetch)) { $msgid=$inbox[msgid]; if ($_POST['chk'.$msgid]) { mysql_query("DELETE FROM inbox WHERE `msgid`='$msgid' LIMIT 1"); $msgcount++; } } if ($msgcount>0) {$message="Messages deleted: ".$msgcount;} else {$message="You didn't select any messages to delete";} } PHP:
I thought you were going to ask that I've just looked at my inbox script and I would post the code that makes them but its VERY long so if you want to hang on a couple secs I'll write some code for you...
Here's some code that will go through my inbox and make a list of checkboxes followed by the inbox subject title: $fetch=mysql_query("SELECT * FROM inbox WHERE userid='$uid' ORDER BY isread ASC, whenin DESC"); $htmInbox=""; while ($inbox=mysql_fetch_assoc($query)) { $htmInbox.='<input type="checkbox" name="chk'.$inbox['msgid'].'" /> '.$inbox['subject'].'<br />'; } PHP: Generates HTML like this... <input type="checkbox" name="chk1" /> A message title<br /> <input type="checkbox" name="chk2" /> Some other message<br /> <input type="checkbox" name="chk3" /> And another<br /> <input type="checkbox" name="chk4" /> One last message<br /> Code (markup): btw, msgid is the unique identifier in my database for that table.
Still i dont understand one thing... How to name checkboxes... because its php not html.... so here is my plan: while (select from db - usernames) $username = username from db echo $username; echo checkbox after submit i want: delete checked usernames from this database count how many boxes were checked write all usernames which were checked HELP HELP HELP
Here's some code for you to play with... <?php if ($_POST['delsel']) { $intCounter=0; $arrDeleted=array(); $query=mysql_query("SELECT `id`,`username` FROM `users` ORDER BY `username` ASC"); while ($row=mysql_fetch_array($query)) { if ($_POST['chk'.$row['id']]) { mysql_query("DELETE FROM `users` WHERE `id`='".$row['id']."' LIMIT 1"); array_pop($row['username']); $intCounter++; } } if ($intCounter>0) { $msg="Messages deleted: ".$intCounter; } else { $msg="You didn't select any messages to delete"; } } ?> <html> <head> <title>User Deleter Thingamebob</title> <style type="text/css"> table.boxed {border: 1px #000000 solid;} table.boxed td {border: 1px #000000 solid;} </style> </head> <body> <?=$msg?><br /><br /> <?php if ($intCounter>0) { echo $arrDeleted[0]; if ($intCounter>1) { for ($i=1;$i<count($arrDeleted);$i++) { echo ', '.$arrDeleted; } } echo '<br /><br />'; } ?> User list: <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table cellspacing="0" class="boxed"> <?php $query=mysql_query("SELECT `id`,`username` FROM `users` ORDER BY `username` ASC"); while ($row=mysql_fetch_assoc($query)) { echo '<tr><td><input type="checkbox" name="chk'.$row['id'].'" /> '.$row['username'].'</td></tr>'; } ?> </table> <input type="submit" name="delsel" value="Delete Selected Users" /> </form> </body> </html> Code (markup): That should do just what you want but I haven't tested it - back up your table before trying it.