tfburges Posted July 3, 2008 Share Posted July 3, 2008 I've tried every combination of parenthesis and apostrophes I can think of and I can't seem to get this to work. I read on another forum that $_POST['field'][$num] is the way to go but it isn't working for me. Maybe it's because I'm using it inside of isset? Basically, I'm using checkboxes to remove certain fields (in a special form creator form lol). And I'm using isset to equate a checked box to a 1 and an unchecked box to a 0. This is the code for the checkbox: for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { echo " <input type=checkbox name=",$delete_field_mach[$cntr]," value=1> "; } This is the code for processing the checkboxes: for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { $delete_field_mach = (isset($_POST['delete_field_mach'][$cntr]) && $_POST['delete_field_mach'][$cntr] == '1')? 1 : 0; if ($delete_field_mach == '1') { mysql_query("DELETE FROM reports WHERE field_desc = '",mysql_result($result_field_desc_mach, $cntr),"'") or die ('MYSQL error: ' . mysql_error()); } } For this particular processing code, the error log doesn't say anything is wrong and it simply doesn't work. If I change it to... (isset($_POST['delete_field_mach'[$cntr]]) ...the error log says "PHP Parse error: syntax error, unexpected '[', expecting ']' in D:\\My Documents\\IRLCM_tech_form\\admin\\loop2.php on line 23, referer: http://xx.xx.xxx.xxx/admin/preview.php" which is expected, I guess. Anyone know the proper syntax for this or a better way to go about it? Oh and also, I've also tried doing this by ID but the way I have the form set up, I would have to go back and change a ton of stuff for it to work. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 3, 2008 Share Posted July 3, 2008 (isset($_POST["delete_field_mach[$cntr]"]) That might be what you're after. Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { echo " <input type=checkbox name=",$delete_field_mach[$cntr]," value=1> "; } Should be this: for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { echo " <input type=checkbox name=\"delete_field_mach[{$cntr}]\" value=1> "; } Also, you can group the IDs of the rows you want to delete in an array, then you only have to use one delete query, e.g.: $delete_ids = array (); for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { $delete_field_mach = (isset($_POST['delete_field_mach'][$cntr]) && $_POST['delete_field_mach'][$cntr] == '1')? 1 : 0; if ($delete_field_mach == '1') { $delete_ids[] = (int) mysql_result($result_field_desc_mach, $cntr); } } if (count ($delete_ids) > 0) { "DELETE FROM reports WHERE field_desc IN (". implode (', ', $delete_ids). ")"; } Quote Link to comment Share on other sites More sharing options...
tfburges Posted July 3, 2008 Author Share Posted July 3, 2008 Hey thanks for the quick replies! (isset($_POST["delete_field_mach[$cntr]"]) That might be what you're after. Yeah I've tried this too (with single quotes as well) and to no avail. for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { echo " <input type=checkbox name=",$delete_field_mach[$cntr]," value=1> "; } Should be this: for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { echo " <input type=checkbox name=\"delete_field_mach[{$cntr}]\" value=1> "; } Ah I think I changed it to that earlier (or something equivalent, i.e. got rid of the $ before delete and isolated $cntr) but had to undo a bunch of things and forgot to do it again. I just tried making only this change again and no worky. :-X Also, you can group the IDs of the rows you want to delete in an array, then you only have to use one delete query, e.g.: $delete_ids = array (); for ($cntr = 0; $cntr < $num_rows_sect_mach; $cntr += 1) { $delete_field_mach = (isset($_POST['delete_field_mach'][$cntr]) && $_POST['delete_field_mach'][$cntr] == '1')? 1 : 0; if ($delete_field_mach == '1') { $delete_ids[] = (int) mysql_result($result_field_desc_mach, $cntr); } } if (count ($delete_ids) > 0) { "DELETE FROM reports WHERE field_desc IN (". implode (', ', $delete_ids). ")"; } I just tried all of this and still nothing. This is pretty frustrating! I'm gonna take a break from it and work on something else. Maybe something will come to me later on... Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 I hope you remembered to use mysql_query for the query I wrote there, I usually just write the query as a string so that people can use whatever wrapper functions they use to do the actual work. I use a hand-written one called execute_query, for example. 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.