Jump to content

Inserting data into MySQL using a form with checkboxes


joelphil

Recommended Posts

hello all,

my goal has been to insert data into
mysql using a form with checkboxes:

Here's my basic form:

<form action="POST" METHOD="insertdata.php">


<INPUT type=checkbox name="choice[]" value="one">One
<INPUT type=checkbox name="choice[]" value="two">Two
<INPUT type=checkbox name="choice[]" value="three">Three
<INPUT type=checkbox name="choice[]" value="four">Four

<INPUT type=submit name=submit value="Submit">


Here is basic table for info"

$sql = Insert into tbl_name (choice1,choice2, choice3, choice4) VALUES (...


you get the picture. so, I have tried counting array and using foreach but, I am unable to
resolve this problem. any help would be great. thanks.

Link to comment
Share on other sites

Something like this:
[code]<?php

if(isset($_POST['submit']))
{
    for($i = 0; $i <= 3; $i++)
    {
        $choice[$i] = isset($_POST['choice'][$i]) ? $_POST['choice'][$i] : 'NULL';
    }

    $sql = "INSERT INTO tbl_name (choice1, choice2, choice3, choice4) VALUES ('" . implode("', '", $choice) . "')";#

    echo "Query built: <code>" . $sql . '</code><br /><br />';
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="post">
<INPUT type=checkbox name="choice[0]" value="one">One
<INPUT type=checkbox name="choice[1]" value="two">Two
<INPUT type=checkbox name="choice[2]" value="three">Three
<INPUT type=checkbox name="choice[3]" value="four">Four
<INPUT type=submit name=submit value="Submit">[/code]
Probably a long route but it does what you want to do.
Link to comment
Share on other sites

Since only those checkboxes that are checked are returned to your script, you should change your form to be something like this:
[code]<INPUT type=checkbox name="choice[1]" value="one">One
<INPUT type=checkbox name="choice[2]" value="two">Two
<INPUT type=checkbox name="choice[3]" value="three">Three
<INPUT type=checkbox name="choice[4]" value="four">Four
[/code]

Then in your script you can do something like:
[code]<?php
if (isset($_POST['choice'])) {  // only do this if any of the boxes were checked
   $tmpq = array();
   foreach($_POST['choice'] as $key => $value)
        $tmpq = 'choice' . $key . " = '" . $value . "'";
   $sql = 'insert into tablename set ' . implode(', ',$tmpq);
   $res = mysql_query($sql) or die("There was a problem with the query: $sql<br>" . mysql_error());
}
?>[/code]
Note, that I've used the alternative format for the Mysql INSERT statement.

Ken
Link to comment
Share on other sites

Wildteen, thanks for your help but, it didn't insert data.

I appreciate your time and help.

-Joel

[!--quoteo(post=387472:date=Jun 24 2006, 10:53 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 24 2006, 10:53 AM) [snapback]387472[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Something like this:
[code]<?php

if(isset($_POST['submit']))
{
    for($i = 0; $i <= 3; $i++)
    {
        $choice[$i] = isset($_POST['choice'][$i]) ? $_POST['choice'][$i] : 'NULL';
    }

    $sql = "INSERT INTO tbl_name (choice1, choice2, choice3, choice4) VALUES ('" . implode("', '", $choice) . "')";#

    echo "Query built: <code>" . $sql . '</code><br /><br />';
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="post">
<INPUT type=checkbox name="choice[0]" value="one">One
<INPUT type=checkbox name="choice[1]" value="two">Two
<INPUT type=checkbox name="choice[2]" value="three">Three
<INPUT type=checkbox name="choice[3]" value="four">Four
<INPUT type=submit name=submit value="Submit">[/code]
Probably a long route but it does what you want to do.
[/quote]
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.