PHP找不到变量来显示我的包含页面上的产品,有什么建议吗?

时间:2012-03-14 18:42:23

标签: php mysql web isset

我正在尝试为我的电子商务网站创建一个php包含文件,该文件位于将显示产品的页面中心。数据库已连接。我有这个,但它一直说“数据使这个页面丢失”。该变量未设置。我是一个相对初学者,我不知道该怎么做。

提前致谢!

// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
    // Connect to the MySQL database  
    include "config.inc.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        // get all the product details
        while($row = mysql_fetch_array($sql)){ 
            $product_name = $row["product_name"];
            $price = $row["price"];
            $details = $row["details"];
            $category = $row["category"];
            $subcategory = $row["subcategory"];
            $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
        }
    } else {
        echo "That item does not exist.";
        exit();
    }
} else {
    echo "Data to render this page is missing.";
    exit();
}

1 个答案:

答案 0 :(得分:1)

您在网址中缺少'id'。你打电话的网址应该是这样的

www.example.com/product.php?id=1

您收到错误,因为脚本告诉您没有'id'值。