1

I have a little question.

I have few stored values in session Array. These values are ID's of products. After that i want to display products from my database, but this is not working propertly for me. Can someone help me a little? :) (I am still learning :) )

<?php
include 'includes/dbconnect.php';
$orderid = $_SESSION['order'];
foreach ($orderid as $value) {
    $sql="SELECT * FROM product WHERE productID LIKE '%$value%'";
    $result=$conn->query($sql);
    while($row=$result->fetch_assoc()){ 
        echo '<tr>';
        echo '<td>'.$row["tag"].'</td>';
        echo '<td>'.$row["price"].',- Kč</td>';
        echo '<td><a href="product.php?id='.$row["productID"].'"><img src="images/'.$row["tag"].'.jpg" width=70"></a></td>';
        echo '<td>1</td>'   ;
        echo '<td><a href="#" class="btn btn-danger btn-lg">X</a></td>';
    }
}
?>

var_dump($orderid); shows:

array(1) {
    ["order"]=> array(10) {
        [0]=> string(2) "44"
        [1]=> string(2) "46"
        [2]=> string(2) "44"
        [3]=> string(2) "54"
        [4]=> string(1) "1"
        [5]=> string(2) "44"
        [6]=> string(1) "1"
        [7]=> string(2) "44"
        [8]=> string(2) "47"
        [9]=> string(2) "74"
    }
}
Fanie Void
  • 331
  • 1
  • 9
Pe Tr
  • 39
  • 4

1 Answers1

0

Just for the purposes of SO, I'll make my comment as an answer:

In the $sql query instead of using LIKE '%$value%'"; use this: LIKE '". $value ."'";

This ensures that we actually get the value of the variable.

Studocwho
  • 2,404
  • 3
  • 23
  • 29
  • This is a massive security bug [waiting to happen](https://xkcd.com/327/). The only way to securely inject user data into a SQL queries is [parameterized queries](https://stackoverflow.com/a/60496/23118). – hlovdal Nov 07 '21 at 18:07