jeudi 8 octobre 2020

How to display both selected/unselected checkbox from mysql database using PHP [duplicate]

i am showing checkbox from the database, and storing selected checkbox in comma seperated value(CSV) format in the database. now i m facing minor issue to show selected and unselected checkbox while editing. Below my code

<?php

$query = "SELECT * FROM genres";
$result = mysqli_query($conn,$query); //connection working fine

while($fetch = mysqli_fetch_array($result))
{
$allcheckbox[] = $fetch["GenreName"]; // all checkbox from database
}
$selectedcheckbox_array = explode(",", $selectedcheckbox);  // already selected checkbox from database
foreach ($allcheckbox as $key => $value)
{?>

<input type="checkbox" name="genres" id="genres" value="<?php echo "$value" ;?>"
<?php if (isset($selectedcheckbox_array) && in_array("$value" , $selectedcheckbox_array))
{echo 'checked="checked"' ; }?>
/>
<label for="genres"><?php echo "$value" ; ?></label><br />

<?php } ?>

now the issue is not able to show all the selected checkbox from the database, only 1st checkbox displaying with checked find below image. only crime is showing with checked other 2 more checkbox (Horror, political) not checked. Let me know how to solve this issue.

enter image description here




Aucun commentaire:

Enregistrer un commentaire