2012-04-15 58 views
1

enter code here初学者到PHP和需要帮助。我通过创建的管理部分添加了产品,但它并未反映在页面上。我从代码中得到meassge:“echo”呈现此页面的数据缺失.Get ID变量未设置。请联系我们。”;”。请帮助数据呈现此页丢失

代码:

<?php 

// Script Error Reporting 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
?> 
<?php 
// Check to see the URL variable is set and that it exists in the database 
if (isset($_GET['?id=1'])) { 
    // Connect to the MySQL database 
    include "..storescripts/connect_to_mysql.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['?id=1']); 
    // 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"); 
    $productsCount = mysql_num_rows($sql); // count the output amount 
    if ($productsCount > 0) { 
     // get all the product details 
     while($row = mysql_fetch_array($sql)){ 
      $product_name = $row["product_name"]; 
      $price = $row["price"]; 
      $shippingprice = $row["shippingprice"]; 
      $details = $row["details"]; 
      $category = $row["category"]; 
      $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
      //product_name, price, shippingprice, details, category, date_added 
     } 

    } else 
    { 
     echo "No product in the system with that ID."; 
     exit(); 
    } 

} else 
{ 
    echo "Data to render this page is missing.Get ID variable is not set. Please contact us."; 
    exit(); 
} 
mysql_close(); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title><?php echo $product_name; ?></title> 
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" /> 
</head> 
<body> 
<div align="center" id="mainWrapper"> 
    <?php include_once("template_header.php");?> 
    <div id="pageContent"> 
    <table width="100%" border="0" cellspacing="0" cellpadding="15"> 
    <tr> 
    <td width="19%" valign="top"><img src="inventory_images/<?php echo $id; ?>.png" width="142" height="188" alt="<?php echo $product_name; ?>" /><br /> 
     <a href="inventory_images/<?php echo $id; ?>.jpg">View Full Size Image</a></td> 
    <td width="81%" valign="top"><h3><?php echo $product_name; ?></h3> 
     <p><?php echo "£".$price; ?><br /> 
     <br /> 
     <?php echo "£".$shippingprice; ?> <br /> 
<br /> 
     <?php echo $details; ?> 
<br /> 
     </p> 
     <form id="form1" name="form1" method="post" action="cart.php"> 
     <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> 
     <input type="submit" name="button" id="button" value="Add to Shopping Cart" /> 
     </form> 
     </td> 
    </tr> 
</table> 
    </div> 
    <?php include_once("template_footer.php");?> 
</div> 
</body> 
/html> 
+0

如果您正在获取'“echo”数据来呈现此页面丢失.Get ID变量未设置。请联系我们。“;”'那么这意味着你的'isset($ _ GET ['?id = 1'])'正在评估为false。这表明'$ _GET ['?id = 1']'没有设置。 – Jack 2012-04-15 08:26:06

回答

0

改变这一行

$id = preg_replace('#[^0-9]#i', '', $_GET['?id=1']); 

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
+0

我改变了上面提到的同样的结果。 – user1334267 2012-04-15 13:07:51

0

的$ _GET超全局是一个名称 - 值数组。所以,如果你的查询字符串?ID = 1,$ _ GET将包含

array('id' => 1) 

,因此你可以检索使用$ _GET [ '身份证'] id的值。然后,如果需要,您可以根据预期值进行检查。

+0

如何设置$ _get ['id = 1']?对不起,我是所有这一切的新手 – user1334267 2012-04-15 13:02:39

+0

@ user1334267 http://www.php.net/manual/zh/language.variables.external.php严肃地说,这是基本的东西。像这样的问题你不可能得到很多帮助。我建议离开并阅读一些关于基本PHP的教程/书籍。 – liquorvicar 2012-04-15 13:11:52