Jump to content

$_POST with an array?


tfburges

Recommended Posts

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.

Link to comment
Share on other sites

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). ")";
}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.