Why is this PDO query not working properly?
$colors = $_GET['color'];
$colors = explode(' ', $colors);
$colors = implode(',',$colors);
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 IN (".$colors.")");
$items ->execute();
while($info = $items->fetch(PDO::FETCH_ASSOC))
{
echo $info['color_base1'];
}
You need to escape the
$colorsor you are subject to a SQL injection attack. There is a little-known PHP functionarray_fillthat is GREAT for this:It appears your problem is that your colors weren’t wrapped with quotes, but that problem goes away in my code because it uses bound parameters.