We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,763 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

multiple checklist sql insert

Hello,


I have this multiple checklist and I cannot figure out how to enter the checked values into the database.

The table is called "colors" and the column name is "color" and is of type "text".

add_color.php

<?php

// connect to database
require "mysql_connect.php";

?>

<?php

// get value from the form
$color = $_POST['color'];

// add it to the database
$sql_query = ("INSERT INTO colors (color) VALUES ($color)");

?>



<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">

<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White

<td><input name="submit" type="submit" value="Add color" />

</form>

I am new to php and sql and I have searched a lot on the internet buy just cannot figure out how to get this to work. Please help me to get this working.

Thanks

2
Contributors
3
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
4
Views
shamed
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

1) $_POST is an array, so you should use foreach.
2) You generate $sql_query (a string), but it is never executed (using mysql_query)

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Ah that makes sense. So the following would work?

<?php

// connect to database
require "mysql_connect.php";

?>

<?php

// get value from the form
$color = $_POST['color'];

foreach($_POST['color'] as $colors){
$insert = mysql_query("INSERT INTO $colors (color) VALUES ($color)");
}


<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">

<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White

<td><input name="submit" type="submit" value="Add color" />

</form>
shamed
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Minor fix:

$colors = $_POST['color'];
foreach ($colors as $color) {
  $insert = mysql_query("INSERT INTO colors (color) VALUES ('$color')");
}
pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0625 seconds using 2.67MB