3

My question is how to retrieve a checkbox value from the database in PHP. The below code is not working.

<td>Hobby</td>
<?php
    $result = mysqli_query($con,"SELECT hobby FROM simple WHERE id = id"); 
    while($row = mysqli_fetch_array($result))
    {
        $focus = explode(",", $row['hobby']);
?>    
<td>
    <input type="checkbox"  name="hobby[]"  value="Cricket"  size="17"   <?php if(in_array("Cricket",$hobby)) { ?> checked="checked" <?php } ?> >Cricket
    <input type="checkbox" name="hobby[]"  value="Music" size="17" <?php if(in_array("Music",$hobby)) { ?> checked="checked" <?php } ?> >Music
    <input type="checkbox" name="hobby[]"  value="Reading"   size="17" <?php if(in_array("Reading",$hobby)) { ?> checked="checked" <?php } ?> >Reading
    <input type="checkbox" name="hobby[]"  value="Study" size="17" <?php if(in_array("Study",$hobby)) { ?> checked="checked" <?php } ?> >Study</td>
<?php
   }
?>
</tr>
2

4 Answers 4

6

Change the statement,

$focus=explode(",",$row['hobby']);

To:

$hobby=explode(",",$row['hobby']);

Also, You can minimize your code with the use of ternary operators instead of if else like:

<td>
  <input type="checkbox"  name="hobby[]"  value="Cricket"  size="17" <?php echo (in_array("Cricket",$hobby)) ? 'checked="checked" : '';?>>Cricket
  <input type="checkbox" name="hobby[]"  value="Music" size="17" <?php echo (in_array("Music",$hobby)) ? 'checked="checked" : '';?>>Music
  <input type="checkbox" name="hobby[]"  value="Reading"   size="17" <?php echo (in_array("Reading",$hobby)) ? 'checked="checked" : '';?>>Reading
</td>

Also, @mapek suggested, your SQL will fetch all records.

$result = mysqli_query($con,"SELECT hobby FROM simple WHERE id = id");

As id will always be equal to id.

You should change it to:

$result = mysqli_query($con,"SELECT hobby FROM simple WHERE id = '$id'");

Another version to minimize this code with arrays:

<td>
<?php
$hobbies = array('Cricket', 'Music', 'Reading');
if (! empty($hobbies)) {
  foreach ($hobbies as $myHobby) {
    $checked = (in_array($myHobby, $hobby)) ? 'checked="checked"' : '';
?>
<input type="checkbox" name="hobby[]" value="<?php echo $myHobby;?>" size="17" <?php echo $checked;?>><?php echo $myHobby;?>
<?php
    }
}
?>
</td>
Sign up to request clarification or add additional context in comments.

Comments

2

try this : many changes required

<td>Hobby</td>
    <?php
        $id= 1; // your input id value
        $result = mysql_query("SELECT hobby FROM simple WHERE id = $id"); 
        while($row = mysql_fetch_array($result))
       {
        $hobby=explode(",",$row['hobby']);

    ?>    
    <td><input type="checkbox"  name="hobby[]"  value="Cricket"  size="17"   <?php if(in_array("Cricket",$hobby)) echo 'checked="checked"'; ?> >Cricket
         <input type="checkbox" name="hobby[]"  value="Music" size="17" <?php if(in_array("Music",$hobby)) echo 'checked="checked"'; ?> >Music
         <input type="checkbox" name="hobby[]"  value="Reading"   size="17" <?php if(in_array("Reading",$hobby))  echo 'checked="checked"'; ?> >Reading
         <input type="checkbox" name="hobby[]"  value="Study" size="17" <?php if(in_array("Study",$hobby))  echo 'checked="checked"'; ?> >Study</td>
       <?php
       }
       ?>
</tr>

16 Comments

please show me output for $result = mysqli_query($con,"SELECT hobby FROM simple");
my simply code<td>Hobby</td> <td><input type="checkbox" name="hobby[]" value="Cricket" >Cricket <input type="checkbox" name="hobby[]" value="Music" >Music <input type="checkbox" name="hobby[]" value="Reading">Reading <input type="checkbox" name="hobby[]" value="Study" >Study</td> </tr>
are you getting output for mysqli_query($con,"SELECT hobby FROM simple"); ? what is it ?
mysqli_query() expects parameter 1 to be mysqli, null given
then something wrong with connection. try mysqli_query("SELECT hobby FROM simple");. pls tell me its output ?
|
1

Change variable name in

<?php if(in_array("Cricket",$hobby)) { ?>

from $hobby to $focus

2 Comments

mysqli_query() expects parameter 1 to be mysqli, resource given
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given
0
$newArray = explode(",",$result['hobbies']);
<input type="checkbox" name ="hobbies[]" value ="reading" <?php if(in_array("reading",$newArray)){ echo "checked";} ?>
<input type="checkbox" name ="hobbies[]" value = "games" <?php if(in_array("games",$newArray)){ echo "checked";}?>>`
<input type="checkbox" name ="hobbies[]" value = "coding"  <?php if(in_array("coding",$newArray)){ echo "checked";} ?>

1 Comment

Not upload your code only, describe your problem to get a answer quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.