-3

I write this code and

$id=$_GET['category_id'];
            echo $id;
            $products=$db->prepare('SELECT * FROM `products` WHERE category_id=4');
            $products->execute();


?>
 <?php 

while ($product=$products->fetch(PDO::FETCH_ASSOC)) {

             ?>

<div id="mehsul">
      <div class="altmehsul">

             <div onclick="show()" class="card" style="width: 15rem;">
                  <div class="photo">
                        <img src="https://i1.rozetka.ua/goods/7373993/53819988_images_7373993442.jpg"
                        class="card-img-top" alt="...">
                  </div>
                  <div class="card-body">
                        <h5 class="card-title"><?php echo $product['name']; ?></h5>
                        <p class="card-text"><?php echo $product['price']; ?></p>
                  </div>
            </div>

          <?php 
}
?>

And this divs appeared but I write instead 'id=3' for example id=$id this dinamic divs is hiding. WHY?

Djeff_C
  • 1
  • 2
  • Specifically, [this answer](https://stackoverflow.com/a/3446245/1941241) will tell you why `category_id=$id` doesn't work in your query. – rickdenhaan Aug 19 '19 at 19:50

1 Answers1

-1

PDO supports placeholder values that make doing this correctly really easy:

$id=$_GET['category_id'];

$products=$db->prepare('SELECT * FROM `products` WHERE category_id=:id');
$products->execute([ 'id' => $id ]);

That's all there is to it.

tadman
  • 208,517
  • 23
  • 234
  • 262