tfburges Posted July 7, 2008 Share Posted July 7, 2008 On the preceding page, the checkboxes are named after the id number. i.e. <input type=checkbox name=1 value=1> <input type=checkbox name=2 value=1> etc. The following code SHOULD look to see which rows were checked; and if they were, the rows are deleted. $qid = "SELECT `id` FROM reports"; if ( ($rid = mysql_query( $qid, $link )) === FALSE ) exit( 'ID Query failed' ); while ( $did = mysql_fetch_assoc( $rid ) ) { $cid = (isset($_POST['$did']) && $_POST['$did'] == '1')? 1 : 0; //a checked box yields a 1; an unchecked box yields a 0 if ($cid == '1') { mysql_query("DELETE FROM `reports` WHERE `id` = '$did'") or die ('MYSQL error: ' . mysql_error()); } } Quote Link to comment Share on other sites More sharing options...
br0ken Posted July 7, 2008 Share Posted July 7, 2008 If I was you I would echo the value in $cid while going through the loop. This way you'll be able to work out if the value is being set in the checkbox and this will help you debug your problem. Another thing, if you insist on error checking a query like this it is generally done using the die() function. <?php $rs = mysql_query($sql) OR die("Query failed: ".mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 7, 2008 Share Posted July 7, 2008 $did = mysql_fetch_assoc( $rid ) this code returns an array you need to go while ( $row = mysql_fetch_assoc( $rid ) ) { $did=$row['id']; $cid = (isset($_POST['$did']) && $_POST['$did'] == '1')? 1 : 0; //a checked box yields a 1; an unchecked box yields a 0 if ($cid == '1') { mysql_query("DELETE FROM `reports` WHERE `id` = '$did'") or die ('MYSQL error: ' . mysql_error()); } Scott. Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 7, 2008 Author Share Posted July 7, 2008 Hmmm... still isn't working. Thanks for the input though guys! I'll come back to it tomorrow and maybe something new will occur to me. Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 8, 2008 Author Share Posted July 8, 2008 I think the question I originally intended to ask was: can I name checkboxes numbers or do they have to start with A-Z? Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 8, 2008 Author Share Posted July 8, 2008 Also, is there a better way to do this? Quote Link to comment Share on other sites More sharing options...
craygo Posted July 8, 2008 Share Posted July 8, 2008 yes there is a better way. Create an array from the checkboxes then you can run a loop to delete the entries form page. make the name an array and the value of the checkbox will be the id of the row <input type=checkbox name=del[] value=$id> <input type=checkbox name=del[] value=$id> then on the page that will delete the rows foreach($_POST['del'] as $del_id){ $sql = "DELETE FROM `reports` WHERE `id` = '$del_id' LIMIT 1"; $res = mysql_query($sql); if(!$res){ echo "Could not delete row with id $del_id.<br>Error: ".mysql_error(); } else { echo "Row deleted<br>"; } } Ray Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 8, 2008 Author Share Posted July 8, 2008 Sadly, that doesn't work with the way I have the page set up. The data is split into sections and isn't necessarily in order, so the ID's don't match up. I'm sure there's a way to get it to match properly though. My first thought is just to change del[] to del[",row['id'],"]. Any objections? I'm gonna try this and a few things later on and post the results. In the mean time, I'm busy with something else. :-X Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2008 Share Posted July 8, 2008 see http://www.phpfreaks.com/forums/index.php/topic,205703.msg933188.html#msg933188 Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 9, 2008 Author Share Posted July 9, 2008 Thanks a bunch! That works... ...but I did have to do this: My first thought is just to change del[] to del[",row['id'],"]. Any objections? Overall: while ( etc... ) { <input type=checkbox name=del[",$datam['id'],"] value=",$datam['id'],"> } ... while ( etc... ) { <input type=checkbox name=del[",$datap['id'],"] value=",$datap['id'],"> } ... while ( etc... ) { <input type=checkbox name=del[",$datap['id'],"] value=",$datap['id'],"> } etc... $idlist = join(',' , $_POST['del']); mysql_query("DELETE FROM `formz` WHERE `id` IN ($idlist)"); 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.