How to insert multiple checkbox values into one mysql table (field) using php
Hi Guys,
Looking for some desperately.
I am new to php, with a lot search I have been able to put together some form of working script. The problem I am now facing is I have multiple checkboxes for the users to select from but the selected checkbox value does not get inserted into the table instead shows as “Array”. With some help I plan to get all the selected checkbox values into one table.
I have two files one "testpage.html" which contains the form and the other "test_post.php" to process the form data. Please find below html and php file codes. Have to // in front of every code in html so the code can be seen.
PHP code: <?php $host="localhost"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="dbname"; // Database name $tbl_name="tblname"; // Table name
// Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form $FullName=$_POST['FullName']; $MobileNumber=$_POST['Number']; $EmailAddress=$_POST['EmailAddress']; $Address=$_POST['Address']; $petathome=$_POST['petathome'];
// Insert data into mysql $sql="INSERT INTO $tbl_name(FullName,Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$petathome')"; $result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='testpage.html'>Back to main page</a>"; }
else { echo "ERROR"; } ?>
<?php // close connection mysql_close(); ?>
Last edited by Inigoesdr; 08-09-2012 at 02:49 AM..
// You have to loop through the array of checked box values ...
$pets="";
foreach($petathome as $entry){
$pets .= $entry.",";
}
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FullName,Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
PHP code: <?php $host="localhost"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="dbname"; // Database name $tbl_name="tblname"; // Table name
// Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form $FullName=$_POST['FullName']; $MobileNumber=$_POST['Number']; $EmailAddress=$_POST['EmailAddress']; $Address=$_POST['Address']; $petathome=$_POST['petathome'];
// You have to loop through the array of checked box values ... $pets=""; foreach($petathome as $entry){ $pets .= $entry.","; }
// Insert data into mysql $sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
// if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='testpage.html'>Back to main page</a>"; }
else { echo "ERROR"; } ?>
<?php // close connection mysql_close(); ?>
Please let me know if you want me to start all over again. With fresh table and form, php file. If so please let me know what is to be done.
Thank you once again
Regards,
mohammedsali
Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tblname"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$FullName=$_POST['FullName'];
$MobileNumber=$_POST['Number'];
$EmailAddress=$_POST['EmailAddress'];
$Address=$_POST['Address'];
$petathome=$_POST['petathome'];
// Loop through the array of checked box values ...
$pets="";
$flag=0;
foreach($petathome as $entry){
$pets .= $entry."|";
$flag=1;
}
if($flag==1){
$pets=rtrim($pets);
}
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FullName, Number, EmailAddress, Address, petathome)VALUES('$FullName', '$Number', '$EmailAddress', '$Address', '$pets')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='testpage.html'>Back to main page</a>";
}
Need one more help with the checkbox is it possible to have multiple check boxes and have one check box with a text filled for user to add their choice which is not part of the check boxes. And this check box, text filed data is added along with the other checkbox values, data.
You'll need a text box.
You will not need to add another checkbox though.
If they put text in the box, that means they want it added to the list.
If they leave the text box blank, it won't get added.
Try this change first.
I'm hoping that using the same name ("petathome[]") for the textbox
will append it to the array, along with the checkbox values.
You are allowing user (form) values to be put into your MySQL table.
That can be a security risk (SQL Injections) ... so escape them first ...
PHP Code:
// Get values from form
$FullName=mysql_real_escape_string($_POST['FullName']);
$MobileNumber=mysql_real_escape_string($_POST['Number']);
$EmailAddress=mysql_real_escape_string($_POST['EmailAddress']);
$Address=mysql_real_escape_string($_POST['Address']);
$petathome=mysql_real_escape_string($_POST['petathome']);
EDIT,
I noticed a type on your Leopard checkbox line.
I fixed it in my form (example above).
It might still be bad in your form.
There is a error when processing the script, all the fields expect the petathome fields are filled.
The error is:
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /database/test_post.php on line 17
Warning: Invalid argument supplied for foreach() in /database/test_post.php on line 22
The full php code is: <?php $host="host"; // Host name $username="username"; // Mysql username $password="password"; // Mysql password $db_name="dbname"; // Database name $tbl_name="tblname"; // Table name
// Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form $Name=mysql_real_escape_string($_POST['Name']); $Number=mysql_real_escape_string($_POST['Number']); $EmailAddress=mysql_real_escape_string($_POST['EmailAddress']); $Address=mysql_real_escape_string($_POST['Address']); $petathome=mysql_real_escape_string($_POST['petathome']);
// Loop through the array of checked box values ... $pets=""; $flag=0; foreach($petathome as $entry){ $pets .= $entry.", "; $flag=1; } if($flag==1){ $pets=rtrim($pets); }
// Insert data into mysql $sql="INSERT INTO $tbl_name(Name, Number, EmailAddress, Address, petathome)VALUES('$Name', '$Number', '$EmailAddress', '$Address', '$pets')"; $result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful". if($result){ echo "Successful"; echo "<BR>"; echo "<a href='testpage.html'>Back to main page</a>"; }
else { echo "ERROR"; } ?>
<?php // close connection mysql_close(); ?>
Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
But when replace checkbox with radio the user is able to select any one radio options but what the enter in "Also:" textbox also gets entered into the database.
Is there a way around this by putting a radio button in front of "Also:" and only if this radio button is select the text filed is enabled? Any thing you think will help will be great.
Regards,
mohammedsali
Last edited by mohammedsali; 08-07-2012 at 08:18 PM..
Why are you inserting many values into a single field?
Normalize that structure and create a second table to handle a many to one / many to many relationship. As it sits right now, you cannot properly select, update, or delete a single petsathome within this dataset without iterating EVERY record that's within it. If you have say 100K records, and you try searching for everyone whom has a Lion at home, you need to get every record in order to determine first if they have a lion at home. This is not ideal; doing so will see the time required even just for selections increment with each record added.
And heaven forbid if you need to delete Lion or update Dog.
I am very new to MySQL and php not sure of how to create relationship(s) and don't think i would like to get into it as it may be very complex for me to handle later on.
As far as the filtering and making changes to petathome goes the data under it is user filled and will not be changed.
I do have a simple, raw .php file to search, filter based on FullName and export the complete database that i would like to make into one, but will come to that next.
Thank you once again Fou-Lu for the suggestions, i really appreciate it
Fou-Lu,
I'm guessing there are so many other things not quite right, there's no
way the OP will be able to process it all. It appears as he said, a learning thing.
But one might say, "learn it right, from the beginning". No way we're going to be
able to do an online course using codingforums. So, I'm just letting it go with the flow.
Mo ...
OK ... so now you're wanting radio buttons instead of checkboxes?
Create the new form with radio buttons, but the value of the one
in front of <text> box will be value="x"
Next, there will be some javascripting to detect if the radio button is
clicked, and then enable the text box. I think you should do some Googling
for: javascript radio button enable text box
See what you find.
If you can get the javascript to enable the text box, the PHP processing
script will see if the radio button value is "x". If it is, it will use the text
in the text box instead of the radio button value. If the radio button
value is NOT "x", it will use the radio button value.