在while循环中从数据库中获取变量

时间:2014-01-10 22:16:24

标签: php mysql

我试图添加创建产品页面,并且我在产品表中包含所有相关的列名称,但循环因某些原因不起作用:(

<?php
 $dbhost = 'localhost';
 $dbuser = 'CU4507408';
 $dbpass = '*********';
 $dbname = 'CU4507408';
 $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
 mysql_select_db($dbname);
 $query  = "SELECT * FROM product WHERE id=".$_REQUEST['productID'].";";
 $result = mysql_query($query) or die("failed!");
 while($row = $query->fetch(PDO::FETCH_ASSOC)){
       $productID = $row['productID'];
       $name = $row['name'];
       $price = $row['price'];
       $manufacturer = $row['manufacturer'];
       $rating = $row['rating'];
       $categoryID = $row['categoryID'];
       $productinfo = $row['productinfo'];
       $image = $row['image'];
       $youtube = $row['youtube'];
 }
?>

我得到的错误是

Undefined index: productID in /home/4507408/public_html/viewproduct.php on line 8 failed!

productID是产品表的主键

谢谢:)

1 个答案:

答案 0 :(得分:0)

$_REQUEST['productID']未定义。

您的网址(如果您使用的是GET)应如下所示:

www.example.com/products?productID=3

如果缺少productID部分,则它将变为未定义。