Mysql query based on checkbox result

1

I'm having trouble creating a mysql query based on checkbox.

1. I have a form based on a select that contains a checkbox for     line

<input type="checkbox" name="check[]" value="<?php echo $linhas['id'];?>" > 

2. I make an implode to get the "id"

$check_id = implode(',', $_POST["check"]);

3. $ check_id var_dump results in the "id" I want (clicked)

var_dump($check_id)."<br>"; // retorno do var_dump é string(3) "1,3"

4. finally I would like to create a query to display the data with     based on the selected "id."

$comprado = mysqli_query($conn, "

            SELECT pedidos.id as id_comprado, qtde
            FROM pedidos
            WHERE id_comprado = '$check_id'

            ");

5. These are the errors displayed

  

Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result,   boolean given in xxx on line 108

My line 108 is $comprados = mysqli_num_rows($comprado);

  

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result,   boolean given in xxx on line 132

My line 132 starts with while($comprados = mysqli_fetch_array($comprado)){...

    
asked by anonymous 28.02.2018 / 16:50

1 answer

0

Solving your problem is simple. In your query, use IN instead of = . This way:

$comprado = mysqli_query($conn, "
    SELECT pedidos.id as id_comprado, qtde
    FROM pedidos
    WHERE pedidos.id IN (".$check_id.")
");

EDIT: I edited the query, now it will work. The alias assigned in AS can not be used in WHERE.

    
28.02.2018 / 17:44