0

我目前正在尝试在我的个人网站上实现购物篮功能,但似乎很挣扎。我已经从我的产品页面添加了链接,并且它应该传递“ID”信息,但是每当我将产品添加到我的购物篮并将我带到我的购物篮页面时,它总是显示“你没有物品在你的购物篮'。这是我的脚本,对此问题的任何帮助将不胜感激。这是我的第一次尝试,我在这方面的 php 技能有限,考虑到这一点,如果有一个非常简单的解决方案,我深表歉意。

    <?php
    if ($_Get[action]=="add") {
$radio_id=$_Get[radio_id];
$_Session[$radio_id]++;
    }

    if($_SESSION[$radio_id]) { 
    echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; 

    foreach($_SESSION[$radio_id] as $radio_id => $quantity) { 

    $result = mysqli_query($link, "SELECT * FROM radios WHERE              radio_id=".$_GET[radio_id]) or die( mysqli_error($link));
    $array = mysqli_fetch_array($result);

    //echo "<p>Manufacturer: " . $array[manufacturer] . "<br>";
    //echo "Model: " . $array[model] . "<br>";
    //echo "Price: " . $array[price] . "<br>";
    //echo "Description: " . $array[description] . "</p>";

    if(mysql_num_rows($result) > 0) {

    list($array[model], $array[manufacturer], $array[description], $array[price]) = mysql_fetch_row($result);

    $line_cost = $array[price] * $quantity;
    $total = $total + $line_cost;

    echo "<tr>";
    echo "<td align=\"center\">$name</td>";
    echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
    echo "<td align=\"center\">$line_cost</td>";
    echo "</tr>";
    }
    }

    echo "<tr>";
    echo "<td colspan=\"2\" align=\"right\">Total</td>";
    echo "<td align=\"right\">$total</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
    echo "</tr>"; 
    echo "</table>";
    }

    else{
    echo "You have no items in your shopping cart.";
    } 
    ?>
4

1 回答 1

1

Few points to be corrected:

  • $_Get should be $_GET.
  • $_Session should be $_SESSION.

Your facing this problem because of incorrect syntax for session. Change $_Session to $_SESSION on line number four. Also use session_start() at beginning of your page, if you haven't started your session .

Updated code:

<?php
session_start();
if ($_GET['action']=="add") {
$radio_id=$_GET['radio_id'];
$_SESSION[$radio_id]++;
}
于 2013-11-14T02:29:58.633 回答