-2

i am trying to make a website for a shop. On the products page, i display general information about all the products loaded from the database. i want to create a separate page that will show different text and image based on which product the user clicks on (i want users to go to this page when they click on any of the products). Is this possible or do i need to code a new page everytime a product is added?

        <?php

            $sql = "SELECT * FROM products;";
            $result = mysqli_query($conn, $sql);
            $resultCheck = mysqli_num_rows($result);

            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            };

            if ($resultCheck > 0) {
                while($row = mysqli_fetch_assoc($result)){
                    echo "<div class='productCard'>";
                    echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';

                    echo    "<h1>" .$row["name"]. "</h1>
                            <h1>" .$row["stock"]. "</h1>
                            </div>";
                    
                }
            } else {
                echo "0 results";
            }

        ?>

3 Answers3

2

It's not only possible, it's very common functionality and is covered by many PHP tutorials. You may have already used one or more tutorials which don't cover this functionality, but you're always encouraged to explore more tutorials and examples.

At a high level, what you'd essentially do is have each link include an identifier for the record you want to display. For example, part of your echo might include something like this:

echo '<a href="product.php?id=' . $row['id'] . '">Click here</a>';

Which would produce something like:

<a href="product.php?id=123">Click here</a>

(There are different ways to do it. Single- vs. double-quotes, heredoc syntax, etc. I personally like to keep the HTML to spec as well which means client-side double-quotes. But any way you choose to emit your markup to the client is fine.)

When the user clicks on the link, the query string parameter id=123 follows the user to that product.php page. Then in the code on that page you'd read the value from the $_GET array:

$id = $_GET['id'];

(Other arrays exist for other kinds of data submitted in a request. URL query string parameters are part of $_GET.)

You'd then use that value in your database query. The query itself might look like this (borrowed from an excellent answer here on a question about SQL injection that's definitely worth a read for beginners):

$stmt = $conn->prepare('SELECT * FROM products WHERE id=?');
$stmt->bind_param('i', $id); // 'i' specifies the variable type => 'integer'
$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Do something with $row
}

Different database technologies bind values to queries in different ways. But as a beginner now is the time to get in the habit of binding values rather than concatenating strings. This is where that aforementioned question/answers about SQL injection becomes extremely useful knowledge.

Within that loop (even if there's only one record, though you might want to put in some checks to ensure there's only one or at least one and respond accordingly otherwise) you can display the data about that record, similar to how you already do for multiple records on your main page.

David
  • 208,112
  • 36
  • 198
  • 279
0

You can use query string variables for this (also known as GET-variables). For instance, in your link you can add i.e. ?id=5to your link, so it becomes something like mysite.com/products?id=5. Then you can use $_GET['id'] to get whatever id the user has in their URL.

https://www.php.net/manual/en/reserved.variables.get.php

Esben Tind
  • 885
  • 4
  • 14
-1

Welcome to stackoverflow! You can use a links to redirect user to detail of product with id parameter.

You can do it by $_GET:

For example this is your code "Products.php":

<?php

        $sql = "SELECT * FROM products;";
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        };

        if ($resultCheck > 0) {
            while($row = mysqli_fetch_assoc($result)){
                echo "<a href='product.php?id=$row["id"]'><div class='productCard'>";
                echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';

                echo    "<h1>" .$row["name"]. "</h1>
                        <h1>" .$row["stock"]. "</h1>
                        </div>";
                
            }
        } else {
            echo "0 results";
        }

    ?>

And in file product.php:

<?php
   $id = $_GET["id"];

   ... and then just use this variable $id for select product
?>

Be carefull of SQL injection:

I recomend to use mysqli_real_escape_string() function or other function and use prepared statements

Marty1452
  • 430
  • 5
  • 19