tedu.edu.vn| How to insert multiple checkbox values into one mysql table (field) using php
89 views

How to insert multiple checkbox values into one mysql table (field) using php

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:

 

 

 



Full Name: 



Number: 



Email Address: 




Address: 



Which animal do you want to keep?

Dog

Cat

Lion

Tiger

Leopard
  

  

 

 

 

 

 

 

 


PHP code:
$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 "
";
echo "Back to main page";
}

else {
echo "ERROR";
}
?> 

// close connection 
mysql_close();
?>

 

 

 

 

 

 

 

Answer:

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


Share with your friend!




© 2017 TEDU LEARNING AND ENTERTAINMENT

NGUYEN MINH HOANG  

01 LE LOI ST, WARD 1, MY THO CITY, TIEN GIANG PROVINCE
Email: minhhoangtgg@gmail.com | Mobile N0:
+84 1255 022 800|

Website: http://tedu.edu.vn