Q:

How to validate set of checkboxes/ array of checkboxes?

after looking for ages on the internet i thought id ask here for help. I have a simple checkbox table here in which i click the checkbox and the result is posted back in PHP, but when i dont click any checkbox and submit it i get the following error.
Notice: Undefined index: idlights in C:\xampp\htdocs\lt4\checkbox\index.php on line 44

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\lt4\checkbox\index.php on line 44

How can i validate this table so that if nothing is checked it just says something like "Please check a checkbox" or something like this? i have tried
if (empty($camid)

{

echo "<p style='color: red'>Please check a checkbox </p>";
}

But that didn't work, Here is my code, Any help on this would be very much appreciated
<?php
            include('conn.php');
                $query=mysqli_query($conn,"select * from `camera`");
                while($row=mysqli_fetch_array($query)){
                    ?>
                    <tr>
                        <td><input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]"></td>
                        <td><?php echo $row['cameras']; ?></td>

                    </tr>
                    <?php
                }
            ?>

        </tbody>
    </table>
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </div>
    <div>
        <h2>You Booked:</h2>
        <?php
            if (isset($_POST['submit'])){
                foreach ($_POST['camid'] as $id):

                $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
                $srow=mysqli_fetch_array($sq);
                echo $srow['cameras']. "<br>";

                endforeach;
            }
        ?>
       ```

A:

Just use an additional if statement like below.
<?php

        if (isset($_POST['submit'])){

            if(isset($_POST['camid'])){

               foreach ($_POST['camid'] as $id):

                $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
                $srow=mysqli_fetch_array($sq);
                echo $srow['cameras']. "<br>";

                endforeach; 
            }

        }
    ?>

