SkullzY88 Posted March 8, 2008 Share Posted March 8, 2008 hey here is the problem am using a javascript for my checkbox1 so that wen it is checked it will check all the other check boxs BUT! in order for the javascript to work i have to name the other checkboxes the fallowing <?PHP echo '<input type="checkbox" name="chk['.$news['id'].']" />'; ?> bascialy the $news['id'] is the ID of each row in the database everything is fine untile it comes to deleting if i named the checkboxes chk[] it wuld work no problem but because there is a numeric in the middle chk[1] <-[example] it refuse to to delete here is the code for the delete action if(isset($_POST['delete'])) { foreach($_POST['chk'] as $value) { $query = mysql_query("DELETE FROM site_news WHERE id='$value'"); echo "<center>News successfully deleted</center>"; echo "<META HTTP-EQUIV=Refresh CONTENT=\"3; URL=./?dir=news&file=delete\">"; } } cant seem to find the answer ive been searching google-php.net-this forum and others and cant seem to figure this out like i said before it works if the checkboxes where named chk[] but not if it was somthing like chk[1]. can anyone help me ? thanks to any replies i appreciate it Quote Link to comment Share on other sites More sharing options...
SkullzY88 Posted March 8, 2008 Author Share Posted March 8, 2008 hey found it lol must of skipped it on php.net handy tip look carefully look more than twice here is it fixed if(isset($_POST['delete'])) { while(list($key,$value) = each($_POST['chk'])) { $query = mysql_query("DELETE FROM site_news WHERE id='$key'"); echo "<center>News successfully deleted</center>"; echo "<META HTTP-EQUIV=Refresh CONTENT=\"3; URL=./?dir=news&file=delete\">"; } } Quote Link to comment Share on other sites More sharing options...
Barand Posted March 8, 2008 Share Posted March 8, 2008 If the checkboxes have the id as the value <?php echo '<input type="checkbox" name="chk[]" value="{$news['id']}" />'; ?> then you can do all the deletions with a single query <?php $deletes = join(',', $_POST['chk'] ); $query = mysql_query("DELETE FROM site_news WHERE id IN ($deletes) "); Quote Link to comment Share on other sites More sharing options...
beam1985 Posted March 9, 2008 Share Posted March 9, 2008 oh my god! it totally works!! you are my HERO!! this was part of my web database mgmt midterm, and you were such a help! kudos! 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.