How to insert multiple checkbox values in database in PHP


We can insert multiple checkbox values in a single column in the database in PHP with the help of an implode function in the form of an array.

How to insert multiple checkbox value in database in PHP

How to insert checkbox array value in the database in PHP

There are two ways we can insert multiple checkbox values in the database.

  • We can insert the checkbox values in the form of an array with the help of implode function.
  • We can insert the checkbox values in the form of multiple rows one by one in the database.

But in this tutorial, we will use the implode function. You can create a table in your existing database. Here, I am using a table named ‘cars’ that consists of four fields:–id,company_name, brand_name, and car_name.

DDL information of the table
————————————

CREATE TABLE `cars` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `company_name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `brand_name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,`car_name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci

connect.php:-

<?php
$servername='localhost';
$username="root";
$password="";
try
{
	$con=new PDO("mysql:host=$servername;dbname=database name",$username,$password);
	$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	//echo 'connected';
}
catch(PDOException $e)
{
	echo '<br>'.$e->getMessage();
}
	
?>

<?php
include('connect.php');
if(isset($_POST['save'])){
	$company_name = $_POST['company_name'];
	$brand_name = $_POST['brand_name'];
	//insert checkbox values
	$cars = implode(',',$_POST['car_name']);
	//insert data into database
	$sql="INSERT INTO cars(company_name,brand_name,car_name)VALUES('$company_name','$brand_name','$cars')";
	$stmt=$con->prepare($sql);
	$stmt->execute();
	echo '<div class="alert alert-success">Submitted Successfully<button class="close" id="cl" onclick="coseMsg()" >✗</button></div>';
	//insertion complete
}	
//get the cars
	$stmt2 = $con->prepare("select * from cars");
	$stmt2->execute();
	$cars = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>

<html>
<head>
<title style="color:#31d818">ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
	
}
</style>
</head>
<body>
	<div class="container">
	<h3><u>Insert multiple checkbox values into single column in PHP</u></h3>
	<br/><br/><br/><br/>
		<form action="" method="post">
			<div class="row">
				<div class="col-sm-6">
					<div class="form-group">
					    <label for="username">Company Name:</label>
					    <input type="text" class="form-control" name="company_name">
					    
					</div>
				</div>
				<div class="col-sm-6">
					<div class="form-group">
					    <label for="phone_no">Brand Name:</label>
					    <input type="text" class="form-control" name="brand_name">
					    
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-6">
					Select Cars:-
				</div>
				<div class="col-sm-6">
					<div class="checkbox">
					  <label><input type="checkbox" required="" name="car_name[]" value="Maruti Alto LXI">Maruti Alto LXI</label>
					</div>
					<div class="checkbox">
					  <label><input type="checkbox" required="" name="car_name[]" value="Maruti Alto K10">Maruti Alto K10</label>
					</div>
					<div class="checkbox">
					  <label><input type="checkbox" required="" name="car_name[]" value="Maruti Alto 800">Maruti Alto 800</label>
					</div>
				</div>
			</div>
			<br/><br/>
			  <button type="submit" class="btn btn-primary" name="save">Submit</button>
		</form>
		<br/>
		<?php
		foreach($cars as $car){
			?>
			<b>Output</b>:    <?php echo $car['car_name'];?>
			<?php
		}
		?>
		<br/>
<!--To display the comma seperated value from database one by one-->
<h3>Display the comma seperated value one by one</h3>
		<?php
		foreach($cars as $car){
			$cars = explode(',',$car['car_name']);
			for($i=0;$i<sizeof($cars);$i++){
				echo $cars[$i].'<br>';
			}
		}
		?>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
	function closeMsg()
	{
		$('#cl').hide();
	}
</script>
</body>
</html>

NOTE*
——–
Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

CONCLUSION:- I hope this article will help you to understand. If you have any doubt then please leave your comment below.


3 thoughts on “How to insert multiple checkbox values in database in PHP”

  1. Greate pieces. Keep posting such kind of info on your page.
    Im really impressed by your site.
    Hello there, You’ve performed a fantastic job. I’ll
    definitely digg it and individually recommend to my friends.
    I’m confident they will be benefited from this website.

    Reply
  2. This design is wicked! You obviously know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it.

    Too cool!

    Reply

Leave a Comment

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

THOR311

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

THOR311

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

THOR311

RATU311

THOR311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311