簡體   English   中英

購物車未顯示商品,是否間隔?

[英]Shopping cart not displaying products, intval?

我正在從我遵循的教程中創建一個鏈接到SQL數據庫的PHP購物車。

下面的代碼應該顯示我所有的產品,但是我相信

$id = intval($_GET['id']);

部分搞砸了。 產品的SKU是Varchar,而不是整數,因此我被認為這是造成“ intval”問題的原因。 我可以插入另一種數據類型來再次顯示我的產品嗎?

<?php

if (isset($_GET['action']) && $_GET['action'] == "add")
{
    $id = intval($_GET['id']);

    if(isset($_SESSION['cart'][$id]))
    {
        $_SESSION['cart'][$id]['quantity']++;
    } 
    else 
    {
        $sql2   = "SELECT * FROM products WHERE SKU={$id}";
        $query2 = mysql_query($sql2);

        if(mysql_num_rows($query2) != 0)
        {
            $row2 = mysql_fetch_array($query2);
            $_SESSION['cart'][$row2['SKU']] = array("quantity => 1, "price" => $row['price']);

        } 
        else 
        {
            $message = "This product id is invalid";
        }
    }
}
?>
<h2 class="message"><?php if(isset($message)){echo $message; ?></h2>
<h1>Product Page</h1>
<table>
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Action</th>
    </tr>

    <?php
    $sql   = "SELECT * FROM products ORDER BY SKU ASC";
    $query = mysql_query($sql)or die(mysql_error());

    while($row = mysql_fetch_assoc($query))
    {
        ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['Description']; ?></td>
            <td><?php echo "£" . $row['price']; ?></td>
            <td><a href="index.php?page=products&action=add&id=<?php echo $row['SKU']; ?>">Add to Cart</a></td>
        </tr>

        <?php
    }
    ?>

</table>

您在這里缺少結尾引號:

$_SESSION['cart'][$row2['SKU']] = array("quantity" => 1, "price" => $row['price']);

您還缺少}

<h2 class="message"><?php if(isset($message)){echo $message; } ?> </h2>

檢查文檔中的intval() ,您將看到當變量不是整數時,此方法返回0。

如果您要搜索此內容,則只需將行更改為:

$id = $_GET['id'];

以字符串形式獲取變量

但是,有兩件事要牢記:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM