MySQL and PHP multiple checklist db insert -
i trying insert value of multiple checklist db column. code not working. can spot problem?
my database consists of table called "colors" , 1 column called "color".
<?php // connect database require "mysql_connect.php"; ?> <?php // value form $color = $_post['color']; foreach($_post['color'] $colors){ $insert = mysql_query("insert 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 <input name="submit" type="submit" value="add color" /> </form>
thanks
this nice way add colors
<?php require "mysql_connect.php"; // connect database $colors=array(); // value form if (isset($_post['color'])) $colors = $_post['color']; foreach($colors $color) { mysql_query ("insert 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>
if (isset($_post['color'])) condition important because prevent indexing error in case array empty
$colors=array(); also, declare variables prevent getting undeclared varibles, previously, in code, happen if user not specify color
remember php server-side , getting errors on php create loopholes attacks. try read php best practices, impotant
hopes helps :-)
Comments
Post a Comment