phpapprentice Posted October 10, 2008 Share Posted October 10, 2008 I am a beginner in PHP. Need Help... The form is working fine but the check box is not, when I choose steak, it will add steak into the FOOD field. when I choose steak and pizza, it will add only pizza. I want to add both steak and pizza into one field separated by comma. this is my form ************* <form action="insert.php" method="post"> <table cellspacing="0" cellpadding="2" width="98%" border="0"> <tbody><tr><td>Firstname:</td><td><input name="firstname" /></td></tr> <tr><td>Lastname:</td><td><input name="lastname" /></td></tr> <tr><td>Age:</td><td><input name="age" /> </td></tr> <tr><td> </td><td><input type="submit" /> </td></tr></tbody></table> <p /><p /><p /><pre> </pre><pre>Gender: Male<input type="radio" name="gender"value="male" /> Female<input type="radio" name="gender" value="female" /></pre><br /> Please choose type of residence::<br />Steak:<input type="checkbox" name="food value="steak"/>:<br /> Pizza:<input type="checkbox" name="food" value="pizza"/>:<br /> Chicken:<input type="checkbox" name="food" />:<br /></form> ******************************* this is my insert.php *********************************** <?php $con = mysql_connect("localhost","mydatabase","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("noyce", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$_POST[food]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> ***************************** my database - SELECT ********************** mysql> select * from Persons; +----------+-----------+----------+------+--------+---------+ | personID | FirstName | LastName | Age | gender | food | +----------+-----------+----------+------+--------+---------+ | 1 | Rommelito | Santos | 29 | NULL | NULL | | 2 | | | 0 | NULL | NULL | | 3 | Mike | | 29 | NULL | NULL | | 4 | Alvin | Garcia | 20 | NULL | NULL | | 5 | John | Berry | 19 | NULL | NULL | | 6 | Jessica | Alba | 30 | | NULL | | 7 | Sharon | Stone | 40 | on | NULL | | 8 | Robin | Padilla | 45 | on | NULL | | 9 | William | Martinez | 45 | on | on | | 10 | Jessa | Zaragosa | 40 | female | on | | 11 | Hanna | Montana | 15 | male | pizza | | 12 | Bon | Jovi | 40 | male | steak | | 13 | Vilma | Santos | 50 | female | chicken | | +----------+-----------+----------+------+--------+---------+ 13 rows in set (0.01 sec) Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/ Share on other sites More sharing options...
dennismonsewicz Posted October 10, 2008 Share Posted October 10, 2008 change every one of your checkbox inputs to look something like this <input type="checkbox" name="food[]" value="pizza"/> Notice the brackets ([]) after the word food Then just post them like this $food = $_POST['food']; Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662154 Share on other sites More sharing options...
Barand Posted October 10, 2008 Share Posted October 10, 2008 your process code would be $food = join (', ', $_POST['food']); // eg "steak, pizza" // insert food into db Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662161 Share on other sites More sharing options...
phpapprentice Posted October 10, 2008 Author Share Posted October 10, 2008 where will i put this code? $food = $_POST['food']; I mark it red in the code. Do i have to add it in my insert.php my INSERT.PHP *********************** <?php $con = mysql_connect("localhost","mydatabase","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("noyce", $con); $food = $_POST['food']; $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$_POST[food]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> ************************************ Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662165 Share on other sites More sharing options...
dennismonsewicz Posted October 10, 2008 Share Posted October 10, 2008 using barand's code use it like this: <?php $con = mysql_connect("localhost","mydatabase","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("noyce", $con); [color=red]$food = join (', ', $_POST['food']); [/color] $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','[color=red]$food[/color]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662166 Share on other sites More sharing options...
dennismonsewicz Posted October 10, 2008 Share Posted October 10, 2008 sorry use it like this (forget the tags) <?php $con = mysql_connect("localhost","mydatabase","mypassword"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("noyce", $con); $food = join (', ', $_POST['food']); $sql="INSERT INTO Persons (FirstName, LastName, Age, gender, food) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[gender]','$food')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662168 Share on other sites More sharing options...
phpapprentice Posted October 10, 2008 Author Share Posted October 10, 2008 PROBLEM SOLVED Thanks dennismonsewicz and Barand. I appreciate it very much. MILLION THANKS!!!!!! phpapprentice Link to comment https://forums.phpfreaks.com/topic/127901-check-box-to-store-into-mysql-database/#findComment-662220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.