为什么我不能在这个关联数组中访问这个值/元素?

时间:2013-04-30 03:25:21

标签: php mysql arrays loops

echo "<br><table><tr>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    if ($j == 2) echo '<tr>';
    echo    '<td width="50%" valign="top"><strong>' . $row['name'] .
            '</strong><br><strong>Price:</strong> £' . $row['price'] .
            '<br><strong>Stock:</strong> ' . $row['stock'] . ' units' .
            '<br><img src="upload/' . $row['imgName'] .
            '" width="100em" height="100em" /><br><br>' . $row['descr'] .
            '<br><a href="added.php?id="' . $row['P_id'] . '">Add to Cart</a></td>';
    $j++;
}

有问题的元素是

  

$行[&#39; P_ID&#39;]

关于P_id的背景信息:

  • 它是产品表的主键
  • 我甚至无法回应它的价值?

修改

根据要求:)

$query = "SELECT * FROM products WHERE prodGroup='$prodGroups[0]'"; // GET PRODUCTS FROM FIRST PRODUCT GROUP
$result = mysql_query($query);

修改

$query = "CREATE TABLE products (
    P_id INT AUTO_INCREMENT NOT NULL KEY,
    name VARCHAR(128) NOT NULL,
    price DECIMAL(5,2) UNSIGNED NOT NULL,
    descr TEXT,
    imgName VARCHAR(50),
    stock INT UNSIGNED NOT NULL DEFAULT '0',
    prodGroup VARCHAR(128) NOT NULL
)";

1 个答案:

答案 0 :(得分:1)

在id =之后删除“,然后再试一次。

<a href="added.php?id="' . $row['P_id'] . '">Add to Cart</a>

应该是

<a href="added.php?id=' . $row['P_id'] . '">Add to Cart</a>