1

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

3
  • 1
    Is echo '<input type="checkbox" name="checked[]"... inside form tags? If not, PHP should be throwing you an error about it, as in "undefined index...". Commented Oct 24, 2016 at 18:48
  • 1
    can you do us a favor and var_dump several versions of $sql so we can see how messed up it is? Commented Oct 24, 2016 at 18:54
  • Sorry, didnt put that in.. yes the checkbox are inside a form tag. I have added it now to the post Commented Oct 24, 2016 at 18:56

2 Answers 2

1

This line probably doesn't work: $check = mysqli_real_escape_string($conn, $_POST['checked']);

$_POST['checked'] is an array. So you have do the line above for each item in the array.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
  ...........

5 Comments

not a fan or "try this" answers, personally. Care to share why they should "try" that? maybe they'll "learn" from their mistake. ;-)
No, sorry that didnt work. I'm not getting any errors, but it just wont echo out any data
Yea, that's what I tried. I've edited the post now so you can see
What is the output if you add print $sel; after $sel = mysqli_real_escape_string($conn, $sel);?
Sorry @krasipenkov, I f*d up. Your version worked perfectly! Thank you :)

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.