-1

I have a page where a user can select multiple check boxes and then submit the form, on the next page it displays what they checked with more details. but my query is not working. how can i pass the checkbox data to the mysql query to get the rest of the data.

this is my query:

$c=$_POST['select'];

$result=mysqli_query($con, 'SELECT * FROM item WHERE item_number IN ("'.implode('","', $c).'")');

echo "<table border='1'>
<tr>
<th>item name</th>
<th>item number</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['item_name'] . "</td>";
  echo "<td>" . $row['item_number'] . "</td>";
  echo "</tr>";
 }
  echo "</table>";


mysqli_close($con);

when running the query i get this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

it returns no results but if i echo the query and copy paste into mysql i get the results, but when running from PHP i get only the error.

any help would be greatly appreciated

2 Answers2

-1

Updated version of your code to put out an error message on the query if there is one. Also sanitises the input

<?php
if (is_array($_POST['select']))
{
    $c = array_map('array_map_callback', $_POST['select']);

    $result = mysqli_query($con, 'SELECT item_name, item_number FROM item WHERE item_number IN ("'.implode('","', $c).'")') OR die(mysqli_error($con));

    echo "<table border='1'>
    <tr>
    <th>item name</th>
    <th>item number</th>
    </tr>";
    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['item_name'] . "</td>";
        echo "<td>" . $row['item_number'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysqli_close($con);
}
else
{
    echo "No items selected";
}

function array_map_callback($a)
{
  global $con;
  return mysqli_real_escape_string($con, $a);
}

?>

Hopefully this should put out the message of what is wrong with the query.

Kickstart
  • 21,251
  • 2
  • 19
  • 33
-4

$c does not seems to be PHP Array, so it can not be imploded here

Ilya
  • 141
  • 6