wabash Posted May 11, 2011 Share Posted May 11, 2011 Hi there, I am trying to insert selections from check boxes into new rows of a mysql table. The idea is to submit a new plant to a database and include it's various flower colors. For example if a plant is submitted with three plant colors from the check box selections, three new rows in the database would be created for that plant. plantcolorlinktable plant_id color_id 1 14 1 15 2 11 2 15 2 16 My html code: <html> <head> <title>Add Plant to Database</title> </head> <body> <form method="post" action="addPlant.php"> <b><big>Plant Insert Admin tool: </b></big><br /><br /> <b> General Plant Info </b> <br /> Enter Plant's Scientific Name: <br /> <input type ="text" name="scientific" size"30" /><br /> Plant Common Name: <br /> <input type ="text" name="common" size"30" /><br /> Plant Family Name: <br /> <input type ="text" name="family" size"30" /><br /> Plant Genus Name: <br /> <input type ="text" name="genus" size"30" /><br /> <b> Plant Flowering Colors: </b> <br /> Red:<input type="checkbox" name="color[]" value="11" >:<br /> Yellow:<input type="checkbox" name="color[]" value="14">:<br /> White:<input type="checkbox" name="color[]" value="15">:<br /> <br /> <input type ="submit" value="Update Database" /> </form> </body> </html> And my php code: <?php $scientific = $_POST['scientific']; $common = $_POST['common']; $family = $_POST['family']; $genus = $_POST['genus']; $color=$_POST['color']; //????? //connect to db require_once 'login.php'; $db_server = mysql_connect($db_hostname,$db_username,$db_password); if (!$db_server) die("Unable to connect to MySQL:" . mysql_error()); mysql_select_db($db_database) or die("Unable to select database:" .mysql_error()); //insert plant data into db $queryplant="INSERT INTO actualplanttable (id, scientific, common, family, genus) VALUES ('NULL', '$_POST[scientific]', '$_POST[common]', '$_POST[family]', '$_POST[genus]')"; mysql_query($queryplant) or die ('Error updating actualplanttable'); // finding last Id inserted $inserted_id = mysql_insert_id(); //insert color data into plantcolorlinktable......I'm not sure what to do here with multiple selections from checkboxes?? $queryid="INSERT INTO plantcolorlinktable (plant_id, color_id) VALUES ('$inserted_id','$color' )"; mysql_query($queryid) or die ('Error updating the plant_id in plantcolorlinktable'); echo "Database Updated With: " .$scientific. " ".$common. " ".$family. " ".$genus. " ".$description; ?> As you can see I'm not sure what to do when it comes to inserting the check box section in my php code Any help would be greatly appreciated. Thanks! Bill.... Quote Link to comment Share on other sites More sharing options...
phppaper Posted May 12, 2011 Share Posted May 12, 2011 foreach ($_POST['color'] as $value) { mysql_query(insert into table...........); } Quote Link to comment Share on other sites More sharing options...
wabash Posted May 12, 2011 Author Share Posted May 12, 2011 Wahoo, perfect. Thanks phppaper Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.