Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the problem in line 19. &id
Undefined variable

The error is in the get query but i do not know how to fix it. it have something with the $_id can anyone help me ?


What I have tried:

HTML
  1  <!DOCTYPE html>
  2  <html lang="en">
  3  <head>
  4      <meta charset="UTF-8">
  5      <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6      <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7      <title>Details</title>
  8      <style>
  9          *{margin: 0;padding: 13px;box-sizing:border-box;}
 10          .container{display: flex;}
 11      </style>
 12  </head>
 13  <body>
 14      <?php
 15      require_once( "./config/db.php");
 16      if(isset($_GET['pr_id'])){
 17          $id = $_GET['pr_id'];}
 18      
 19        $sql = "SELECT * FROM product WHERE pr_id= ". $id ;
 20        $result = mysqli_query( $connect, $sql);
 21        if ($result)
 22        {
 23            while ($row = mysqli_fetch_assoc($result))
 24            {?>
 25                <div class="container">
 26                    <div class="pr-left">
 27                        <img src="./image/<?php echo $row['image'] ?>">
 28                    </div>
 29                    <div class="pr-right">
 30                        <h1><?php echo $row['pr_name'] ?></h1>
 31                        <h3>Price: <?php echo $row['price'] ?>VND</h3>
 32                        <button>BUY NOW</button>
 33                        <p>GIAO HÀNG TOÀN QUỐC <br> THANH TOÁN KHI NHẬN HÀNG <br> ĐỔI HÀNG TRONG 15 NGÀY</p>
 34                    </div>
 35                </div>
 36            <?php
 37            }
 38        }
 39        else{echo 'loi';} 
 40      ?>
 41  </body>
 42  </html>
Posted
Updated 17-Mar-22 23:09pm
v2
Comments
Richard Deeming 18-Mar-22 5:09am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

1 solution

You need to learn about the scope of variables.
PHP
if(isset($_GET['pr_id'])){
    $id = $_GET['pr_id']; // $id only exists within the scope of this block
}
// $id no longer exists here
  $sql = "SELECT * FROM product WHERE pr_id= ". $id ;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900