Ask Question

Menu

How to insert multiple checkbox value in a single column of MySQL using PHP


Here we using 2 file for insert multiple checkbox value:

index.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="process.php">
<input type="checkbox" name="vehicle[]" value="Bike">I have a bike <br>
<input type="checkbox" name="vehicle[]" value="Cycle">I have a Cycle <br>
<input type="checkbox" name="vehicle[]" value="Car">I have a car <br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

process.php

<?php
$url='127.0.0.1:3306';
$username = "root";
$password = "";
$dbname = "admin";
$checkbox1 = $_POST['vehicle'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk.= $chk1.",";
}
$conn = mysqli_connect($url, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO checkbox(vehicle)VALUES( '$chk' )";
if(mysqli_query($conn,$sql)) {
echo 'Data added sucessfully';
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

Output

I have a bike
I have a Cycle
I have a car







Subscribe with us to get latest topic update






Choose a Language



Subscribe