checkbox

How to Insert Multiple Selected Checkbox Values in Database in PHP

Spread the love

Today we will see How to Insert Multiple Selected Checkbox Values in Database in PHP.

Multiple entries into the database in PHP are sometimes very useful. We have to take help of checkbox to do multiple entry in the form and we have to do that in PHP array, I will tell you in details how to do multiple entry in database.

Multiple Selected Checkbox Values in Database in PHP

Step 1: Create a new database and create a new table under it

create new Database. Database name is abcd.

CREATE TABLE `reg` (
  `id` int(11) NOT NULL,
  `username` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL,
  `language` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Step 2: Create a index.php File.

Create an index.php file and create a form in it.

 
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>How To Checkbox Entry Add in PHP </title>
</head>
<body>

<h1>how to insert multiple selected checkbox values in database in php</h1>

<form action="demo.php" method="POST">
<table border="2">
	<tr>
		<td>Name:</td>
		<td><input type="text" name="username"></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type="email" name="email"></td>
	</tr>
	<tr>
		<td>language:</td>
		<td><input type="checkbox" value="Gujrati" name="language[]">Gujrati <br>
		 <input type="checkbox" value="Hindi" name="language[]">Hindi <br>
		 <input type="checkbox" value="English" name="language[]">English <br>
		 <input type="checkbox" value="Marathi" name="language[]">Marathi</td>
	</tr>
	 <tr>
	 	<td colspan="2" align="center"><input type="submit" name="submit"></td>
	 </tr>
</table>
</form>
 

</body>
</html>

Step 3: Create an demo.php File.

Create a demo.php file now write the PHP code in it



<?php 
error_reporting(0);
$connect=mysqli_connect('localhost','root','','abcd');

if (isset($_POST['submit'])) {
$username=$_POST['username'];
$email=$_POST['email'];
$language=$_POST['language']; 
$xyz=implode(",",$language);
//echo $xyz;

$sql="INSERT INTO `reg`  VALUES ('$id','$username','$email','$xyz')";
$query=mysqli_query($connect,$sql);
if ($query) {
	echo "Data Insrted Successfully";
}else{
	echo "Data Not Insrted";
}
}


Spread the love

Related Posts

One thought on “How to Insert Multiple Selected Checkbox Values in Database in PHP

Leave a Reply

Your email address will not be published.