corillo181 Posted January 14, 2007 Share Posted January 14, 2007 alright i'm working on a messeges system ad i'm wondering how do i make it so when the people check the boxes every box selected allt he value in mysql is deleted..[x]-msg1[]-msg2[x]-msg3[x]-msg4[]-msg5[x]-msg6[delete]in the other end what would be my query to make the x msgs to be deleted? Quote Link to comment Share on other sites More sharing options...
trq Posted January 14, 2007 Share Posted January 14, 2007 If you make each checkbox pass the id of the record to delete, your php might look something like...[code]<?php // connect foreach($_POST as $id) { mysql_query("DELETE FROM tbl WHERE id = '$id'"); }?>[/code]Of course you'll need to impliment error checking and the like, but that'll give you the idea.[/code] Quote Link to comment Share on other sites More sharing options...
corillo181 Posted January 14, 2007 Author Share Posted January 14, 2007 thanx if that works i can get the rest to work. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted January 14, 2007 Author Share Posted January 14, 2007 this is something i came up with but i dont know how to pass the checkbox value to the $_post for the each statement [code]$querypms=mysql_query("SELECT * FROM mypms")or die(mysql_error());?><form action="<?php $_SERVER['PHP_SELF']?>" method="post" ><?phpwhile($echopm=mysql_fetch_array($querypms)){?><input name="<?php echo $echopm['id'];?>" type="checkbox" value="<?php echo $echopm['id'];?>" /><?php echo $echopm['subject'];?><br /><?php}?> <input name="Submit" type="submit" id="Submit" value="Delete" /></form><?phpif($_POST['Submit']){foreach($_POST as $del){$del=mysql_query("DELETE * FROM mypms WHERE id='$id'");if($del){echo "done";}}}?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 14, 2007 Share Posted January 14, 2007 [code]<?php if ($_POST['Submit']) { foreach($_POST as $id) { if ($id != 'Delete') { if (mysql_query("DELETE * FROM mypms WHERE id='$id'")) { echo "done<br />"; } } } }?>[/code] Quote Link to comment Share on other sites More sharing options...
corillo181 Posted January 14, 2007 Author Share Posted January 14, 2007 [code]<?php include_once 'db.php';$querypms=mysql_query("SELECT * FROM mypms")or die(mysql_error());?><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ><?phpwhile($echopm=mysql_fetch_array($querypms)){?><input name="<?php echo $echopm['id'];?>" type="checkbox" value="<?php echo $echopm['id'];?>" /><?php echo $echopm['subject'];?><br /><?php}?> <input name="Submit" type="submit" id="Submit" value="Delete" /></form><?php if ($_POST['Submit']) { foreach($_POST as $id) { if ($id != 'Delete') { if (mysql_query("DELETE * FROM mypms WHERE id='$id'")) { echo "done<br />"; } } } } ?>[/code]it does nothing at all.. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 14, 2007 Share Posted January 14, 2007 All those methods can use an enormous number of queries! Remeber you are probably limited to 50 queries per script run - so if someone selects 51 you aint gonna get teh results you wanted.if you declare each check box like so..[code]<?phpwhile($echopm=mysql_fetch_array($querypms)){?><label><input name="delete[]" type="checkbox" value="<?php echo $echopm['id'];?>" />Delete <?php echo $echopm['subject'];?></label><br /><?php}?>[/code]Then to delete all the mesages where the user has checked the corresponding box simply do this..[code]<?php$ids = implode(',',$_POST['delete']);$qry = "DELETE FROM `mypms` WHERE `id` IN(" . $ids . ")";$qry = mysql_query($qry);?>[/code]Beatuy of that little lot is there far les code - no loops - AND it will delete how ever many posts are selected - not limited to the number of queries you can execute. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted January 14, 2007 Author Share Posted January 14, 2007 work nicely but i think i have to separate the code into 2 page becuase when i delete something it would stay there until the page is refreshed. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 14, 2007 Share Posted January 14, 2007 That code should run BEFORE you output any html!Stuff like updates,inserts, deletes etc. shoudl (normally) be done before any output of html. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted January 14, 2007 Author Share Posted January 14, 2007 oh i see now.. thanx Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.