Retrieve more than 3 data's from mysql into checkbox

后端 未结 3 1562
悲&欢浪女
悲&欢浪女 2021-01-22 07:40

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. P

相关标签:
3条回答
  • 2021-01-22 08:12

    Checkbox input itself does not display any text. You need to show both checkbox and text separately like this:

    <input type='checkbox' name='check[]' value='$hobby'><label>$hobby</label>
    
    0 讨论(0)
  • 2021-01-22 08:33

    Try this:

    <?php
         $query = "SELECT * FROM hobbies";
         $result = mysqli_query($con, "$query");
         while ($r=mysqli_fetch_array($result))
         {
              $hobby=$r["hobby_name"];
              echo "<input type='checkbox' name='check[]' value='".$hobby."'><label>".$hobby."</label>";
         }
    ?>
    
    0 讨论(0)
  • 2021-01-22 08:39

    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
    }
    
    0 讨论(0)
提交回复
热议问题