Jump to content

multiple insert using checkboxes


john_6767

Recommended Posts

i'm trying to make an insert page that adds in a client and also adds into the client_areas table the areas that the client is interested in.. (these areas are displayed as checkboxes from the area table..) so i have to insert the client and then insert multple times into the client_areas joining table. I have use a script in asp before and it works exactly right but now i am using php i cannot work out how to do it. (script is in asp and called 'Insert/Update Multiple Tables', i found it here http://charon.co.uk/files.aspx?categoryid=15&filecategoryid=10). Anyone know of any tutorials or code to do this in php?
Link to comment
Share on other sites

one of the easiest ways i've found to do this is simply to name all the associated checkboxes as an array and to loop through them all on the processing page:
[code]
<?php
if (isset($_POST['submit'])) {
  // check to see at least one of your "myCheck" boxes was submitted
  if (isset($_POST['myCheck']) && count($_POST['myCheck']) > 0) {
    foreach ($_POST['myCheck'] as $val) {
      // now, here is where you would insert your value into your database
    }
  }
}

echo "<form name=\"test\" action=\"\" method=\"post\">\n";
for ($i = 1; $i <= 10; $i++) echo "<input type=\"checkbox\" name=\"myCheck[]\" value=\"$i\" /> $i<br />\n";
echo "<input type=\"input\" name=\"submit\" value=\"Submit\" />\n";
echo "</form>\n";
?>
[/code]

hope this helps
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.