checkbox

How to UPDATE Multiple Selected Checkbox Values in the Database in PHP

Spread the love

Today we see how to update multiple selected checkbox values in a database in PHP, before that you should insert multiple values and if you don’t know how to insert multiple values then I have explained in the previous blog how to do it Is.

How to Insert Multiple Selected Checkbox Values in Database in PHP

Click here

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

to create a 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.


<?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";
}
}?>
 <!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 UPDATE multiple selected checkbox values in database in php</h1>
<form action="#" 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>



<h1>SHOW DATA HERE</h1>





<?php 
$connect=mysqli_connect('localhost','root','','abcd');
$sql2="SELECT * FROM reg";
$query2=mysqli_query($connect,$sql2);
?>
<table border="2">
<tr>
<th>id</th>
<th>username</th>
<th>email</th>
<th>lang</th>
<th>language</th>
</tr>
	<?php
while ($row=mysqli_fetch_assoc($query2)) {
	?>
<tr>
	<td><?php echo $row['id'] ?></td>
	<td><?php echo $row['username'] ?></td>
	<td><?php echo $row['email'] ?></td>
	<td><?php echo $row['language'] ?></td>
	<td><a href="update.php?id=<?php echo $row['id'] ?>"> Update </a></td>
</tr>

	<?php
}
?>
</table>

</body>
</html>    

Step 3: Create an update.php File.

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


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>
		how to insert multiple selected checkbox values in database in php
	</title>
</head>
<body>

<?php 
$connect=mysqli_connect('localhost','root','','abcd');
if ($connect) {
	echo "done";
}else{
	echo "error";
}
 
if (isset($_POST["update"])) {
	$id=$_POST['id']; 
$username=$_POST['username'];
$email=$_POST['email'];
$language=$_POST['language'];
	$xyz=implode(",",$language);
	$sql2="UPDATE `reg` SET `id`='$id',`username`='$username',`email`='$email',`language`='$xyz' WHERE id='$id'";
	$con=mysqli_query($connect,$sql2);
if ($con) {
 echo " <script> location.replace('index.php'); </script>" ;
//echo "update";
}else{
echo "error";
}
} 

 
	
$mid=$_GET['id'];
$sql="SELECT * FROM reg WHERE id='$mid' ORDER BY id DESC";
$data=mysqli_query($connect,$sql);
while ($result=mysqli_fetch_array($data)) {
 
$a=$result['language'];
$b=explode(",", "$a");

print_r($b);
 
?>
<form action="#" method="POST">
<table border="2">
<tr>
	<th><input type="text" name="id" value="<?php echo $result['id']; ?>" placeholder="name"></th>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="username"  value="<?php echo $result['username']; ?>"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" name="email"  value="<?php echo $result['email']; ?>"></td>
</tr>
<tr>
<td>language:</td>
<td><input type="checkbox" value="Gujrati" name="language[]"
<?php 
	if (in_array("Gujrati", $b)) {
		echo "checked";
	}
	?>
	>Gujrati <br>
<input type="checkbox" value="Hindi" name="language[]"
<?php 
	if (in_array("Hindi", $b)) {
		echo "checked";
	}
	?>
>Hindi <br>
<input type="checkbox" value="English" name="language[]"
<?php 
	if (in_array("English", $b)) {
		echo "checked";
	}
	?>
>English <br>
<input type="checkbox" value="Marathi" name="language[]"
<?php 
	if (in_array("Marathi", $b)) {
		echo "checked";
	}
	?>
>Marathi</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="update"></td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html> 

Note:To update multiple selected checkbox values in the database in PHP we have to convert the data from string to array using implode

The implode() function returns a string from the elements of an array


Spread the love

Related Posts

Leave a Reply

Your email address will not be published.