Jump to content


Photo

Inserting data into MySQL using a form with checkboxes


  • Please log in to reply
5 replies to this topic

#1 joelphil

joelphil
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 June 2006 - 03:12 PM

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.



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 June 2006 - 03:53 PM

Something like this:
<?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">
Probably a long route but it does what you want to do.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 June 2006 - 03:57 PM

Since only those checkboxes that are checked are returned to your script, you should change your form to be something like this:
<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

Then in your script you can do something like:
<?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());
}
?>
Note, that I've used the alternative format for the Mysql INSERT statement.

Ken


#4 joelphil

joelphil
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 June 2006 - 04:13 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Something like this:
<?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">
Probably a long route but it does what you want to do.
[/quote]


#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 June 2006 - 04:19 PM

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.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 June 2006 - 04:23 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users