Jump to content

Archived

This topic is now archived and is closed to further replies.

joelphil

Inserting data into MySQL using a form with checkboxes

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.

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Thats because you need to add the code in for that! Which is the following:
mysql_query($sql) or die(mysql_error());

I thought you had that, thats why I left it out.

Share this post


Link to post
Share on other sites
Did you look at the sample code I posted? It's almost the same a wildteen's with a few differences with how I construct the query.

Ken

Share this post


Link to post
Share on other sites

×

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.