Siddhu
Siddhu

Reputation: 37

Can't select multiple checkboxes in while loop

After using while loop,I only select first checkbox but I can't select multiple checkboxes in page. Plz solve this issue.

$get=mysqli_query($con,"select * from subjects where cour_id='$id'") or die(mysqli_error($con));
    while($data=mysqli_fetch_array($get))
    {
        ?>
        <div class="be-checkbox">
        <input id="check" type="checkbox" name="chk[]" value="<?php echo $data['sub_name'];?>">
        <label for="check"><?php echo $data['sub_name'];?></label>
        </div>
       <?php
    }

Upvotes: 0

Views: 503

Answers (1)

Koray K&#252;pe
Koray K&#252;pe

Reputation: 736

It should be like this:

$get=mysqli_query($con,"select * from subjects where cour_id='$id'") or die(mysqli_error($con));
    while($data=mysqli_fetch_array($get))
    {
        ?>
        <div class="be-checkbox">
        <input id="check<?php echo $data['id'];?" type="checkbox" name="chk[]" value="<?php echo $data['sub_name'];?>">
        <label for="check<?php echo $data['sub_name'];?"><?php echo $data['sub_name'];?></label>
        </div>
       <?php
    }

Make unique your input ids. Change id if your query doesn't have an id field. Just use something unique. I also recommend you to use PDO library of PHP instead of mysqli_query. It provides an OOP way to connect your database and it makes your queries more secure.

Upvotes: 2

Related Questions