sveini sveini - 1 month ago 9
HTML Question

Select multiple rows from table using checkboxes

I'm trying to use checkboxes for multiple sql query with SELECT.

I have 3 tables, one table for groups one for users and one to connect the user to different groups called groupreluser. Based on what groups are checked, I want it to print out the phonenumber of each user that are a member of those groups.

The checkboxes are created based on the content of the table groups

<form method="post">
<?php
$sql = "SELECT * FROM groups WHERE groupname LIKE '%minor%'";
$result = $conn->query($sql);

if($result->num_rows > 0){

while($row = $result->fetch_assoc()){

echo '<input type="checkbox" name="checked[]" value="'.$row['group_id'].'">'
. $row['groupname'] . '</br>';

}

}
?>
<input type="submit" name="submit" value="submit">
</form>


Code with the sql query to get the phonenumbers.

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

$check = $_POST['checked'];

if(!empty($check)){


foreach($check as $sel){
$sel = mysqli_real_escape_string($conn, $sel);


$sql = "

SELECT
groups.group_id,
groups.groupname,
groupreluser.user_id,
groupreluser.group_id,
users.user_id,
users.name,
users.phone
FROM
groupreluser
JOIN
groups
ON
groups.group_id = groupreluser.group_id
JOIN
users
ON
users.user_id = groupreluser.user_id
WHERE
groups.group_id = '$sel'

";

$res = $conn->query($sql);

if($res->num_rows > 0){

while($row = $res->fetch_assoc()){

echo $row['phone'] . '</br>';

}

}
}
}
}


It works fine as long as I dont use the checkboxes but adds the group_id manually in the SELECT statement. Any idea of why this isn't working? Am I missing someting? Dont know if this is the best way to do it though...
Let me know if this is unclear, and I'll try to explain it better

Answer

Your $_POST['submit'] is array so directly passing it to mysqli_real_escape_string won't work. But this approach will do the work:

if(isset($_POST['submit'])){
$check = $_POST['check'];
if(!empty($check)){

        foreach($check as $sel) {
        $sel = mysqli_real_escape_string($conn, $sel);

        $sql = "

                        SELECT
                                groups.group_id,
                                groups.groupname,
                                groupreluser.user_id,
                                groupreluser.group_id,
                                users.user_id,
                                users.name,
                                users.phone
                        FROM
                                groupreluser
                        JOIN
                                groups
                        ON
                                groups.group_id = groupreluser.group_id
                        JOIN
                                users
                        ON
                                users.user_id = groupreluser.user_id
                        WHERE 
                                groups.group_id = '$sel'

                ";

                $res = $conn->query($sql);
  ...........