PHP Explode: How to Retrieve Multiple Checkbox value from Database in PHP Using Explode

In this article, you going to see How to Retrieve Multiple Checkbox value from Database in PHP using Explode step by step and in detail.

If you are looking for this topic then you are at right place.

Let’s start with the topic regarding How to Retrieve Multiple Checkbox value from Database in PHP Using Explode.

Before start with the topic.

First we going to discuss regarding PHP explode.

It break down the string into an array.

Syntax for explode function is given below.

explode(separator,string,limit)
ParameterDetails
separatorthe separator is Required. Represent from where to split the string
stringthe string is Required. Represent string to break.
limitthe limit is Optional. Represent the number of array elements to return.
Syntax PHP Explode Details

How to retrieve multiple checkbox value from database in PHP using explode

With the help of example we going to check this.

To do this first create database name "phptutorial".

In that database, create table name "vehicle".

Execute below command to create table in database.

CREATE TABLE vehicle1 (
    id int NOT NULL AUTO_INCREMENT,
    company varchar(500) NOT NULL,
    vehicle varchar(500),
    PRIMARY KEY (id)
);
Table Created and Database Name
Table Created and Database Name

If you want know more about phpMyAdmin, check with below link book which is a step-by-step instructional guide to get you started easily with phpMyAdmin and teach you to manage and perform database functions on your database.

Mastering phpMyAdmin 3.4 for Effective MySQL Management

OR,

Mastering PHP, MySQL and JavaScript: A Step-by-Step Guide to Creating Dynamic Websites

Now our next point is to create file named config.php file database connection.

Also you can add code for database connection in the the top of file.

Below is code for database connection.

<?php 
	$servername = "localhost"; 
	$username = "root"; 
	$password = ""; 
	$dbname = "phptutorial"; 
	// Create connection 
	$conn = mysqli_connect($servername, $username, $password, $dbname); 
	// Check connection 
	if (!$conn) { 
		die("Connection failed: " . mysqli_connect_error()); 
	} 
?>

For database connection you have to provide server name, username, password, and database name.

Now our next step is pass the checkbox value to database.

Before that we have to check with below point.

Which tag is used to create a checkbox in a form?

The input type checkbox tag is used to create a checkbox in a form.

Main point regarding checkbox is used for selecting one or more choices.

Checkbox is display as square box.

It is a activated, when it is ticked.

Which tag creates a checkbox for a form in html?

Below is code snippet for checkbox tag.

<input type="checkbox"> defines a checkbox.

How to add checkbox in html form?

Now our next point is adding of checkbox in html form.

Using below code snippet for adding checkbox.

<form action="" method="post">
  <label>Company</label>
  <select name="company">
	<option value="">Please choose company</option>
	<option value="Bajaj">Bajaj</option>
	<option value="Tata">Tata</option>
	<option value="BMW">BMW</option>
  <select><br/><br/> 
  <label>Vehicle with you :</label><br/><br/>	
  <input type="checkbox" name="vehicle[]"  value="Bike">
  <label for="vehicle1"> I have a Bike</label><br>
  <input type="checkbox" name="vehicle[]" value="Jeep">
  <label for="vehicle2"> I have a Jeep</label><br>
  <input type="checkbox" name="vehicle[]" value="Boat" checked>
  <label for="vehicle3"> I have a Boat</label><br><br>
  <input type="submit" name="submit" value="Submit">
</form> 
How to add checkbox in html form
How to add checkbox in html form

On Submit form, all the input type field is post the data and it get inserted into database.

Note : Use all checkbox same name as 'vehicle[]' with square bracket.

This checkbox value is post on submitting form and insert into database.

Use the insert statement to insert the value into the database.

To retrieve multiple checkbox value from database use the select statement.

As all checkbox value is insert as a single comma separated value.

How to explode checkbox value in PHP?

Use the below the code snippet for getting multiple checkbox value using explode.

<?php 
	// Explode under while loop.
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
	 echo '<PRE>'; print_r($var);
?>

Below is the complete code snippet for retrieve multiple checkbox value from database.

<?php 
	$servername = "localhost"; 
	$username = "root"; 
	$password = ""; 
	$dbname = "phptutorial"; 
	// Create connection 
	$conn = mysqli_connect($servername, $username, $password, $dbname); 
	// Check connection 
	if (!$conn) { 
		die("Connection failed: " . mysqli_connect_error()); 
	} 
	if(isset($_POST['submit']) && $_POST['submit'] =='Submit' && $_POST['company']!=''){
		$company = $_POST['company'];
		$vehiclearr = $_POST['vehicle'];
		$vehicle =implode(',',$vehiclearr); 
		// SQL query 
		// output data of each row
		$sql = "INSERT INTO `vehicle` (`id`, `company`, `vehicle`) VALUES ('','$company','$vehicle');";
	 if (mysqli_query($conn, $sql)) { 
			echo "New record created successfully"; 
		} else { 
			echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
		} 
	}
?>
<?php 
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
?>
<!DOCTYPE html>
<html>
<body>
<div style="background-color:#e5bcbc;" align="center">
<h1>Retrieve Multiple Checkbox value from Database in PHP</h1>

<form action="" method="post">
  <label>Company</label>
  <select name="company">
	<option value="">Please choose company</option>
	<option value="Bajaj">Bajaj</option>
	<option value="Tata">Tata</option>
	<option value="BMW">BMW</option>
  <select><br/><br/> 
  <label>Vehicle with you :</label><br/><br/>	
  <input type="checkbox" name="vehicle[]" <?php echo (in_array("Bike", $var)?'checked':''); ?> value="Bike">
  <label for="vehicle1"> I have a Bike</label><br>
  <input type="checkbox" name="vehicle[]" <?php echo (in_array("Jeep", $var)?'checked':''); ?> value="Jeep">
  <label for="vehicle2"> I have a Jeep</label><br>
  <input type="checkbox" name="vehicle[]" <?php echo (in_array("Boat", $var)?'checked':''); ?> value="Boat" checked>
  <label for="vehicle3"> I have a Boat</label><br><br>
  <input type="submit" name="submit" value="Submit">
</form>
<br/>
<div align="center">
<table class="table table-bordered" border="2" align="center" width="50%" >
<tr> <td colspan="5"><center><h3>Output Vehicles Inserted in Database</h3></center></td></tr>
  <tr> 
      <tr><center><th>ID</th><th>Company</th><th>Vehicle</th></center></tr>
    <?php $fetchqry = "SELECT * FROM vehicle"; 
    $result1= mysqli_query($conn,$fetchqry);
    $num = mysqli_num_rows($result1);
    if($num > 0){
    while($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)){ ?>
      <tr><td><p><?php echo $row['id']; ?></p></td><td><?php echo $row['company']?> </td>
      <td><?php echo $row['vehicle']; ?></td></tr>  
      <?php
    }
    } 
    ?>
  </tr>
  <tr>
	<td>
		<?php 
			echo '<PRE>'; print_r($var);
		?>
	</td>
  </tr>
</table>
</div>
</div>
</body>
</html>

OUTPUT :

How to Retrieve Multiple Checkbox value from Database in PHP Using Explode
How to Retrieve Multiple Checkbox value from Database in PHP Using Explode

How to show multiple checkbox checked in PHP from database?

Now we going to see displaying of multiple checkbox checked in PHP from database.

Using ternary operator using you can achieve this.

After fetching the data from the database for multiple checkbox as data for multiple checkbox is save as comma separated.

Used explode function to convert the single comma separated data into array.

Used the below code snippet for it.

<?php 
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
?>

Then use the ternary operator to checked the multiple checkbox.

Use below code snippet to achieve this.

 <input type="checkbox" name="vehicle[]" <?php echo (in_array("Bike", $var)?'checked':''); ?> value="Bike">
  <label for="vehicle1"> I have a Bike</label><br>
  <input type="checkbox" name="vehicle[]" <?php echo (in_array("Jeep", $var)?'checked':''); ?> value="Jeep">
  <label for="vehicle2"> I have a Jeep</label><br>
  <input type="checkbox" name="vehicle[]" <?php echo (in_array("Boat", $var)?'checked':''); ?> value="Boat">
  <label for="vehicle3"> I have a Boat</label><br>

OUTPUT:

How to show multiple checkbox checked in PHP from database
How to show multiple checkbox checked in PHP from database

How to get multiple checkbox value in PHP using array?

Go through the above all code snippet you come to understand regarding this.

Below is the code snippet for getting multiple checkbox value in PHP using array.

<?php 
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
	 print_r($var);
?>

The output from above code snippet you will get array form.

Array
(
    [0] => Jeep
    [1] => Boat
)

How to explode checkbox value in PHP?

Execute the select query to fetch comma separated checkbox value from database.

Use the while loop to get comma separated data and use explode to get checkbox value.

Below is code snippet for explode.

<?php 
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
	 print_r($var);
?>

How to fetch data from database in PHP and display in checkbox?

Use the below code snippet to fetch data from database in PHP and display in checkbox.

<?php 
	 $lastsqlRes = "SELECT LAST_INSERT_ID(), company, vehicle FROM vehicle";
	 $result2= mysqli_query($conn,$lastsqlRes);
	 while($row = mysqli_fetch_array($result2,MYSQLI_ASSOC)){
	 $var = explode(",",$row['vehicle']);		 
	 }
?>

Use ternary operator to display in checkbox.

Please refer below code.

<input type="checkbox" name="vehicle[]" <?php echo (in_array("Bike", $var)?'checked':''); ?> value="Bike">

How to upload multiple images in PHP and store in database

Conclusion:

Using above code snippet you can understand regarding PHP explode and its uses.

I hope you like this article and if you have any query, please comment below.

Leave a Comment