×

Please Login or Register to continue.

2
(3.6K Views)

I want to insert the HTML checkbox value in the MYSQL database using PHP. I have many checkboxes in my form. I want to insert all checkbox values in the MYSQL database in PHP. 

I can create HTML checkboxes in a form - 

<form action="" method="post">
<label>HTML </label><input type="checkbox" name="language" value="HTML">
<label>C Programming</label><input type="checkbox" name="language" value="C Programming"> 
<label>Java</label><input type="checkbox" name="language" value="Java"> 
</form>

As you can see above, I created three checkboxes. I want to insert all of these HTML checkbox values into the database using PHP. 

(4.3K Points)
in PHP

Share

2 Answers
(3.4K Points)
(edited)
1

You can insert multiple checkbox values at a time using the foreach loop and array. We create multiple checkboxes to give multiple options. First of all, we create an MYSQL database table. We take three fields - id, language title, and post id. The post id can be any page id or other id. You can make it dynamic. 

How to insert multiple checkbox values in PHP?

We can insert multiple checkbox values using the array. First of all, we create an MYSQL database table. 

CREATE TABLE `languages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `langTitle` varchar(100) DEFAULT NULL,
  `postId` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

In the table above, we have created three fields. The "langTitle" column will be used to insert values from the HTML checkboxes. 

Let's create an HTML form containing multiple checkboxes. 

<form action="" method="post">
	Languages:<br> 
<label>HTML </label><input type="checkbox" name="langTitle[]" value="HTML">
<label>C Programming</label><input type="checkbox" name="langTitle[]" value="C Programming"> 
<label>Java</label><input type="checkbox" name="langTitle[]" value="Java"> 
<input type="submit" value="Submit" name="submit_form">
</form>

There are three HTML checkboxes inside the HTML form tag. As you can see, we created an array to put all the checked values in the same array. We will hold the array using PHP POST method and loop by foreach() loop while inserting data into the MYSQL database 

Let's hold the data and insert it into the database. 

<?php 
$databaseHost = 'localhost';
$databaseName = 'tute'; 
$databaseUsername = 'root'; 
$databasePassword = '';  
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
if(isset($_POST['submit_form']))
{
$langTitle = $_POST['langTitle'];
$postId=12;//Set your post id
foreach ($langTitle as $lang) {
$result = mysqli_query($mysqli,"INSERT INTO languages(langTitle,postId) values('$lang','$postId')");
}
if($result)
{
echo "Inserted Successfully.";
}
else{
echo "Something went wrong.";
}
}
?>

You can make postid dynamic according to your need. In the code above, we created a connection using mysqli_connect() function. You can use the OOPS concept here. We hold the data by PHP post method and insert using the foreach loop. In this way, you can insert multiple checkbox data using PHP and MYSQL.


Comment

(9.4K Points)
(edited)
1

If you want to insert checkbox values in the MYSQL database then you should create a form with multiple checkboxes.

For example - 

<form action="" method="POST">
	Countries:<br> 
<label>India</label><input type="checkbox" name="countries[]" value="India">
<label>America</label><input type="checkbox" name="countries[]" value="America"> 
<label>England</label><input type="checkbox" name="countries[]" value="England"> 
<input type="submit" value="Submit" name="sub">
</form>

Kindly write the same array() countries[] for all fields. It's just like, we are putting multiple checkbox values into an array.

Use PHP code and POST checkbox data using the POST method 

$countries=$_POST['countries'];

Here, "countries" is an array variable that contains single or multiple checkbox values.

Use the PHP foreach loop to insert values one by one like this - 

foreach ($countries as $countryName) {
//Your insert query here - Insert variable value $countryName
}

Use the countryName variable in your insert query. Insert the checked country using the insert query.


Comment

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..