CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   How to insert multiple checkbox values into one mysql table (field) using php (http://www.codingforums.com/showthread.php?t=269717)

mohammedsali 08-06-2012 08:41 PM

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.

Really appreciate your help in advance :p.

HTML form:
PHP Code:

<!DOCTYPE html>
<html>
<body background="blue">
<form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" /><br />
<br />
Number: <input name="Number" type="text" id="Number" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
<br />
Address: <input name="Address" type="text" id="Address" /><br />
<br />
Which animal do you want to keep?<br />
<input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
<input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
<input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
<input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
<input type="checkbox" name="petathomel[]" value="Leopard" />Leopard
  
<input type="submit" name="submit" value="Submit" />
  
</form>
</body>
</html>



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();
?>


mlseim 08-06-2012 10:06 PM

$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')";

mohammedsali 08-06-2012 10:39 PM

Hi Mlseim,

Thanks a ton for the reply.

Still seem to be facing a problem, after entering the above into the php file and get the message:

ERROR

Any suggestions?

Thanks & Regards,
mohammedsali

mlseim 08-07-2012 12:28 PM

Repost your entire script again with the changes made.

mohammedsali 08-07-2012 12:59 PM

Hi Mlseim,

Thank you for taking the time to help me :D

The html and php code is as below.

HTML form:
PHP Code:

<!DOCTYPE html>
<html>
<body background="blue">
<form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" /><br />
<br />
Number: <input name="Number" type="text" id="Number" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
<br />
Address: <input name="Address" type="text" id="Address" /><br />
<br />
Which animal do you want to keep?<br />
<input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
<input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
<input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
<input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
<input type="checkbox" name="petathomel[]" value="Leopard" />Leopard

<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html>



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

mlseim 08-07-2012 04:03 PM

Try this:

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'];

// 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>";
}

else {
echo 
"ERROR";
}
?>

<?php
// close connection
mysql_close();
?>




.

mohammedsali 08-07-2012 05:01 PM

Hi mlseim,

That worked like a charm. Thanks a ton :D

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.

Thanks again you are a life saver :D

Regards,
mohammedsali

mlseim 08-07-2012 06:35 PM

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.

PHP Code:

<!DOCTYPE html>
<
html>
<
body background="blue">
<
form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" /><br />
<
br />
Number: <input name="Number" type="text" id="Number" /><br />
<
br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
<
br />
Address: <input name="Address" type="text" id="Address" /><br />
<
br />
Which animal do you want to keep?<br />
<
input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
<
input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
<
input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
<
input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
<
input type="checkbox" name="petathome[]" value="Leopard" />Leopard<br />
Also: <input type="text" name="petathome[]"><br />
<
input type="submit" name="submit" value="Submit" />

</
form>
</
body>
</
html

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.

<input type="checkbox" name="petathomel[]" value="Leopard" />Leopard



.

mohammedsali 08-07-2012 07:28 PM

Hi mlseim,

Thanks again :D

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 html code is:
PHP Code:

<!DOCTYPE html>
<html>
<body background="blue">
<form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" /><br />
<br />
Number: <input name="Number" type="text" id="Number" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" /><br /><br />
<br/>
Address: <input name="Address" type="text" id="Address" /><br />
<br />
Which animal do you want to keep?<br />
<input type="checkbox" name="petathome[]" value="Dog" />Dog<br />
<input type="checkbox" name="petathome[]" value="Cat" />Cat<br />
<input type="checkbox" name="petathome[]" value="Lion" />Lion<br />
<input type="checkbox" name="petathome[]" value="Tiger" />Tiger<br />
<input type="checkbox" name="petathome[]" value="Leopard" />Leopard<br />
Also: <input type="text" name="petathome[]"><br />
<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html>  

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();
?>


mlseim 08-07-2012 07:45 PM

Change this line:
$petathome=mysql_real_escape_string($_POST['petathome']);

To this:
$petathome=$_POST['petathome'];


Add the line in red ...

if($flag==1){
$pets=rtrim($pets);
}
$pets=mysql_real_escape_string($pets);

mohammedsali 08-07-2012 08:15 PM

Hi mlseim,

Works perfectly, thank you :D

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

Fou-Lu 08-07-2012 08:51 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.

mohammedsali 08-07-2012 09:46 PM

Hi Fou-Lu,

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 :D

Regards,
mohammedsali

mlseim 08-07-2012 10:27 PM

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.



.

mohammedsali 08-07-2012 11:39 PM

Hi mlseim,

Thank you for pointing me the right direction :D

Found a script and was able to make changes to fit my html file and it works :)

The changes where needed only in the html file

Html code:
PHP Code:

<!DOCTYPE html>
<
html>
<
body background="blue">
<
form action="test_post.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" required="required" /><br />
<
br />
Number: <input name="Number" type="text" id="Number" required="required" /><br />
<
br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" required="required" /><br /><br />
<
br/>
Address: <input name="Address" type="text" id="Address" required="required" /><br />
<
br />
Which animal do you want to keep?<br />
<
script type="text/javascript">
var 
currentEnabled null;
function 
enableElement(elem) {
if (
currentEnabled) {
currentEnabled.disabled true;
}
elem.disabled false;
currentEnabled elem;
}
</script>
<form action="">
<input type="radio" name="petathome[]" value="Dog"
onclick="enableElement(this.form.elements['inp1']);"> Dog
<br>
<input type="radio" name="petathome[]" value="Cat"
onclick="enableElement(this.form.elements['inp2']);"> Cat
<br>
<input type="radio" name="petathome[]" value="Lion"
onclick="enableElement(this.form.elements['inp3']);"> Lion
<br>
<input type="radio" name="petathome[]" value="Tiger"
onclick="enableElement(this.form.elements['inp4']);"> Tiger
<br>
<input type="radio" name="petathome[]" value="Leopard"
onclick="enableElement(this.form.elements['inp5']);"> Leopard
<br>
<input type="radio" name="petathome[]" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled">

<br>
<br />

<input type="submit" name="submit" value="Submit" />

</form>
</body>
</html> 



All times are GMT +1. The time now is 10:40 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.