Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-06-2012, 08:41 PM   PM User | #1
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
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 .

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

Last edited by Inigoesdr; 08-09-2012 at 02:49 AM..
mohammedsali is offline   Reply With Quote
Old 08-06-2012, 10:06 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
$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')";
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-06-2012)
Old 08-06-2012, 10:39 PM   PM User | #3
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
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
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 12:28 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Repost your entire script again with the changes made.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 12:59 PM   PM User | #5
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi Mlseim,

Thank you for taking the time to help me

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

Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 04:03 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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();
?>



.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 05:01 PM   PM User | #7
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

That worked like a charm. Thanks a ton

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

Regards,
mohammedsali
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 06:35 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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



.

Last edited by mlseim; 08-07-2012 at 06:40 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 07:28 PM   PM User | #9
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Thanks again

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

Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 07:45 PM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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);
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 08:15 PM   PM User | #11
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Works perfectly, thank you

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..
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 08:51 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 09:46 PM   PM User | #13
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
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

Regards,
mohammedsali
mohammedsali is offline   Reply With Quote
Old 08-07-2012, 10:27 PM   PM User | #14
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.



.

Last edited by mlseim; 08-07-2012 at 10:41 PM..
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
mohammedsali (08-07-2012)
Old 08-07-2012, 11:39 PM   PM User | #15
mohammedsali
New Coder

 
Join Date: Aug 2012
Posts: 22
Thanks: 12
Thanked 0 Times in 0 Posts
mohammedsali is an unknown quantity at this point
Hi mlseim,

Thank you for pointing me the right direction

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> 

Last edited by Inigoesdr; 08-09-2012 at 02:50 AM..
mohammedsali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:06 PM.


Advertisement
Log in to turn off these ads.