Php 仅将项目数量呈现给购物车

Php 仅将项目数量呈现给购物车,php,Php,我正在尝试呈现特定项目的数据。首先,我使用了一个代码来呈现它的数量和ID,以及它的工作 代码如下: <?php //render the cart for the user to view $cartOutput = ""; if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shoppi

我正在尝试呈现特定项目的数据。首先,我使用了一个代码来呈现它的数量和ID,以及它的工作

代码如下:

<?php
//render the cart for the user to view
$cartOutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
    }else{
        $i = 0;
        foreach($_SESSION["cart_array"] as $each_item) {
            $i++;
            $item_id = $each_item['item_id'];
            $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
            while($row = mysql_fetch_array($sql)){
                $product_name = $row["product_name"];
                $price = $row["price"];

            }
            $cartOutput .= "<h2>Cart Item $i</h2>";


            while(list($key, $value) = each($each_item)) {
                $cartOutput .= "$key: $value<br>";
                }
            }
        }
?>

我猜要么是查询失败,要么是您使用的字段名错误。尝试添加这段代码以检查这两种可能性

$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");

if ( $sql === false ) {
    echo 'Query Failed: ' . mysql_error();
}

while($row = mysql_fetch_array($sql)) {
    print_r($row);   // check the field names in this array

    $product_name = $row["product_name"];
    $price = $row["price"];

}
附加建议

试试这个,我只是看到输出代码不在while循环中。并不是说您实际上需要一个while循环,因为您已将查询限制为仅返回一个结果
LIMIT 1

<?php
//render the cart for the user to view
$cartOutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
}else{
    $i = 0;
    foreach($_SESSION["cart_array"] as $each_item) {
        $i++;
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while($row = mysql_fetch_array($sql)){
            // no real point in moving these values to another variable
            // just to print them
            //$product_name = $row["product_name"];
            //$price = $row["price"];

            $cartOutput .= "<h2>Cart Item $i</h2>";
            $cartOutput .= "Item ID : " . $each_item['item_id'] . "<br>";
            $cartOutput .= "Item Quantity : " .$each_item['quantity'] . "<br>";
            $cartOutput .= "Item Name : " . $row["product_name"] . "<br>";
            $cartOutput .= "Item Price : Php. " . $row["price"] . "<br>";
        }    
    }
}
?>

问题可能在于变量的范围。您在while循环内获得产品名称和价格的价值,并在while循环外引用它。在foreach语句上方定义变量,而在foreach语句下方定义循环,如$product_name=''。希望这能对您有所帮助。它不起作用。它会通知我,在第53行的/home/a9562316/public_html/cart.php中有未定义的变量行,我应该在哪里添加它?或者我应该用这个来更改代码吗?这是你的代码,添加了几行。应该很明显。哦,谢谢。我试过了,但它仍然只给了我数量。我的页面显示通知,在第68行和第69行中的product_name和price处有未定义的变量。
是否打印($row)
显示带有值的数组,
$row['product_name']
$row['price']
请参阅其他建议
<?php
//render the cart for the user to view
$cartOutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
}else{
    $i = 0;
    foreach($_SESSION["cart_array"] as $each_item) {
        $i++;
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while($row = mysql_fetch_array($sql)){
            // no real point in moving these values to another variable
            // just to print them
            //$product_name = $row["product_name"];
            //$price = $row["price"];

            $cartOutput .= "<h2>Cart Item $i</h2>";
            $cartOutput .= "Item ID : " . $each_item['item_id'] . "<br>";
            $cartOutput .= "Item Quantity : " .$each_item['quantity'] . "<br>";
            $cartOutput .= "Item Name : " . $row["product_name"] . "<br>";
            $cartOutput .= "Item Price : Php. " . $row["price"] . "<br>";
        }    
    }
}
?>