Jebil Jebil - 7 months ago 11
PHP Question

Retrieve more than 3 data's from mysql into checkbox

I need to retrieve the hobbies name from

mysql
and display it in check boxes. I done the below given code. But it displays just check box and not any hobby names. Please help.

$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result))
{
$hobby=$r["hobby_name"];?>
<input type='checkbox' name='check[]' value='$hobby'>
}

Answer

You need to add simple text $hobby next to every checkbox.

Corrected code:

$query = "SELECT * FROM hobbies";
$result = mysqli_query($con, "$query");
while ($r=mysqli_fetch_array($result)) {
 $hobby=$r["hobby_name"];
?>
<input type='checkbox' name='check[]' value='<?php echo $hobby;?>'> <?php echo $hobby;?>
<?php
}