how to get multiple checkbox value from database in php?

how to get multiple checkbox value from database in php? – Use the insert statement to insert the value into MySQL the database. To get multiple checkbox value from MySQL database use the select Query / statement. As all the available checkbox value is insert as a single comma separated value.

how to get multiple checkbox value from database in php?

get and display multiple checkbox values from database using php – How to retrieve one or multiple checkbox values from database using php and MySQL?

Table structure

CREATE TABLE `websites` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `website` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;	

Create a new config.php file.

<?php

$host = "localhost";
$user = "itsolutionstuck_9";
$password = "[email protected]"; 
$dbname = "itsolutionstuck";

$con = mysqli_connect($host, $user, $password,$dbname);
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}

index.php

<?php
include "config.php";
?>
<!doctype html>
<html>
  <head>

  <?php
  if(isset($_POST['submit'])){

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

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

      $checkEntries = mysqli_query($con,"SELECT * FROM websites");
      if(mysqli_num_rows($checkEntries) == 0){
        mysqli_query($con,"INSERT INTO websites(website) VALUES('".$web."')");
      }else{
        mysqli_query($con,"UPDATE websites SET website='".$web."' ");
      }
 
    }

  }
  ?>
  </head>
  <body>
  <form method="post" action="">
    <span>Select websites</span><br/>
    <?php

    $checked_arr = array();

    $fetchWeb = mysqli_query($con,"SELECT * FROM websites");
    if(mysqli_num_rows($fetchWeb) > 0){
      $result = mysqli_fetch_assoc($fetchWeb);
      $checked_arr = explode(",",$result['website']);
    }

    $websites_arr = array("infinityknow","pakainfo","itsolutionstuck","w3hub");
    foreach($websites_arr as $website){

      $checked = "";
      if(in_array($website,$checked_arr)){
        $checked = "checked";
      }
      echo '<input type="checkbox" name="web[]" value="'.$website.'" '.$checked.' > '.$website.' <br/>';
    }
    ?>
 
    <input type="submit" value="Submit" name="submit">
  </form>

  </body>
</html>

Insert Multiple Checkbox Value in Database Using PHP

index.php

<html>  
<body>  
   <form action="" method="post" enctype="multipart/form-data">  
   <div style="width:500px;border-radius:6px;margin:0px auto">  
<table border="1">  
   <tr>  
      <td colspan="2">Select Website:</td>  
   </tr>  
   <tr>  
      <td>Infinityknow</td>  
      <td><input type="checkbox" name="websites[]" value="infinityknow"></td>  
   </tr>  
   <tr>  
      <td>Pakainfo</td>  
      <td><input type="checkbox" name="websites[]" value="pakainfo"></td>  
   </tr>  
   <tr>  
      <td>ItSolutionStuck</td>  
      <td><input type="checkbox" name="websites[]" value="itsolutionstuck"></td>  
   </tr>  
   <tr>  
      <td>w3hub</td>  
      <td><input type="checkbox" name="websites[]" value="w3hub"></td>  
   </tr>  
   <tr>  
      <td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>  
   </tr>  
</table>  
</div>  
</form>  
<?php  
if(isset($_POST['sub']))  
{  
	$host="localhost"; 
	$username="itsolutionstuck_123";
	$word=""; 
	$db_name="itsolutionstuck";
	$tbl_name="itsolutionstuck";  
	$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");
	$checkbox1=$_POST['websites'];  
	$web_name="";  
	foreach($checkbox1 as $chk1)  
	{  
	  $web_name .= $chk1.",";  
	}  
	$in_ch=mysqli_query($con,"insert into itsolutionstuck(website_name) values ('$web_name')");  
	if($in_ch==1)  
	{  
	  echo'<script>alert("Inserted Successfully")</script>';  
	}  
	else  
	{  
	  echo'<script>alert("Failed To Insert")</script>';  
	}  
}  
?>  
</body>  
</html>  

Leave a Reply

Your email address will not be published.