Jump to content

Array from form to DB insert


lurking

Recommended Posts

I have a form that lists a bunch of names with a checkbox next to each one. If the checkbox is checked, the data is inserted into my DB.  Whereas previously, the checkbox value was just a variable (user_ID), now I need it to be an array to pass two variables (user_ID + db_designation):

[code]<input name="user_ID[]" type="checkbox" id="user_ID[]"
value="<?php echo urlencode(serialize(array($row_rsPhysicianDetails['user_ID'],
$row_rsPhysicianDetails['db_designation']))); ?>" />[/code]

I grab the array data via:
[code]<?php $user_id_array = $_POST['user_ID']; ?>[/code]

and then have been attempting to insert into my database with a foreach() and nested if() loop.

[code]<?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "attendance")) {
  foreach($user_id_array as $value)
  {
  list($user, $designation) = unserialize($value);
  if ($designation == "p") {
  $query = "INSERT INTO course_complete (user_ID, course_ID, course_date, credit_hours, course_session)" .
"VALUES($user, '$c_ID', '$c_date', '$credit_hours', '$course_session')";
  @mysql_query($query);
  }
  }
?>
[/code]

At this time, the insert is not working.  Also, when I tried to see if the data was even being collected via:

[code]
<?php
$user_id_array = $_POST['user_ID'];
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
[/code]

the result was an empty array:

Array
{
}

I would appreciate any ideas for making sure the variable $user_id_array is populating appropriately, and then making the insert work with the foreach, if, serialize and list syntax.  Thank you.
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.