PHP 8 Checkbox Value Store In Database Example

03-Apr-2023

.

Admin

PHP 8 Checkbox Value Store In Database Example

Hello Friends,

Now, let's see example of PHP 8 Checkbox Value Store In Database Example. I explained simply about PHP 8 Checkbox Set to Check Based on Database Value Example. I would like to show you Get checked Checkboxes value with PHP 8 MySQL. we will help you to give example of PHP Checkbox Checked from Database Example.

I will give you simple example of PHP 8 - Checkbox Checked With Database Value .

So, let's see bellow solution:

index.php


<!doctype html>

<html lang="en">

<head>

<!-- Bootstrap CSS -->

<title>PHP 8 Checkbox Value Store In Database Example</title>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<?php

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$db = 'shope';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass , $db) or die($conn);

if(isset($_POST['submit'])){

if(!empty($_POST['lang'])) {

$lang = implode(",",$_POST['lang']);

// Insert and Update record

$checkEntries = mysqli_query($conn,"SELECT * FROM languages");

if(mysqli_num_rows($checkEntries) == 0){

mysqli_query($conn,"INSERT INTO languages(language) VALUES('".$lang."')");

}else{

mysqli_query($conn,"UPDATE languages SET language='".$lang."' ");

}

}

}

?>

<div class="container mt-5">

<div class="row">

<div class="col-md-12">

<div class="card w-75 m-auto">

<div class="card-header bg-info text-white text-center">

<h4>PHP 8 Checkbox Value Store In Database Example - Nicesnippets.com</h4>

</div>

<div class="card-body">

<form method="post" action="">

<?php

$checked_arr = array();

// Fetch checked values

$fetchLang = mysqli_query($conn,"SELECT * FROM languages");

if(mysqli_num_rows($fetchLang) > 0){

$result = mysqli_fetch_assoc($fetchLang);

$checked_arr = explode(",",$result['language']);

}

// Create checkboxes

$languages_arr = array("PHP","JavaScript","jQuery","AngularJS");

foreach($languages_arr as $language){

$checked = "";

if(in_array($language,$checked_arr)){

$checked = "checked";

}

echo '<div class="form-check">

<input type="checkbox" name="lang[]" class="form-check-input" style="width: 20px;height: 20px;left: 10px;top: -1px;" value="'.$language.'" '.$checked.' > '.$language.'

</div><br> ';

}

?>

<hr>

<input type="submit" value="Submit" name="submit" class="btn btn-primary">

</form>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

Output:

It will help you...

#PHP 8