Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 购物车无法提取产品ID_Php_Html_Database_Cart_Product - Fatal编程技术网

Php 购物车无法提取产品ID

Php 购物车无法提取产品ID,php,html,database,cart,product,Php,Html,Database,Cart,Product,shop.php <!DOCTYPE html> <html lang="en"> <head> <title>My first Web page</title> <meta charset="utf-8" /> <link href="css/bootstrap.css" rel="stylesheet" type="text/css" /> &l

shop.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>My first Web page</title>
        <meta charset="utf-8" />
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="css/final_index.css" rel="stylesheet" type="text/css" />
    <link href="css/grid.css" rel="stylesheet" type="text/css" />



</head>
<body>

    <div class="container">

        <div class="header">
            <div class="span1"> </div>
            <header>
                <img id="banner" src="images/safc.banner.PNG" alt="banner">
            </header>

        </div>

        <div class="banner1">

        </div>
<br />
        <div class="navbar">
            <div class="navbar-inner">
                <a class="brand" href="#">Sunderland</a>
                <ul class="nav">
                    <li class="active">
                        <a href="final_index.php">Home</a>
                    </li>
                    <li>
                        <a href="shop.php">Shop</a>
                    </li>
                    <li>
                        <a href="login.php">Login</a>
                    </li>
                    <li>
                        <a href="reg.php">Register</a>
                    </li>
                    <li>
                        <a href="protect.php">Protected Page</a>
                    </li>
                    <li>
                        <a href="admin.php">Admin</a>
                    </li>
                </ul>
            </div>
        </div>

        <div class="login1">
            <div id="login1"

            <div class="grid20"> 



<a href="shop.php?cat=Clothes">Clothes</a>
<a href="shop.php?cat=Accessories">Accessories</a>

<div class="shop2"

<div class="grid20"

<div id="s"

</br>

        <?php
             $connection=mysqli_connect('localhost','c3364964','goldie123','c3364964');
        $cat = $_GET ['cat'];

echo $cat. " available displayed here";

?>

<?php

print "<table border=3px cellpadding=12px >";

print "<th>ProductID</th>";

print "<th>Name</ th>";

print "<th>Price</ th>";

print "<th>Image</ th>";

print "<th>Add to cart</ th>";

if ($cat == 'Clothes'){

$query = "SELECT * FROM Products WHERE Type = 'Clothes' ORDER BY ProductPrice DESC";

$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result))

{

echo " <tr>";

echo "<td>" . $row['ID'] . "</td>";

echo "<td>" . $row['ProductName'] . "</td>";

echo "<td>" . $row['ProductPrice'] . "</td>";

echo "<td>" . '<img width=50px height=50px src="./images/' . $row['ProductImage'] . '"  />' . "</td>";

echo "<td><a href=\"cart.php?action=add&id=$ID\">Add To Cart</a></td>";
//echo "<td>" . '<a href="AmendProduct.php?id='. $row['ID'].'">Amend</a>' . "</td>";

//echo "<td>" . '<a href="DeleteProduct.php?id='. $row['ID'].'">Delete</a>' . "</td>";

echo "</tr>";

}

print "</table>";

}

else {

$query = "SELECT * FROM Products WHERE Type = 'Accessories' ORDER BY ProductPrice";

$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result))

{

echo " <tr>";

echo "<td>" . $row['ID'] . "</td>";

echo "<td>" . $row['ProductName'] . "</td>";

echo "<td>" . $row['ProductPrice'] . "</td>";

echo "<td>" . '<img width=50px height=50px src="./images/' . $row['ProductImage'] . '" />' . "</td>";

echo "<td><a href=\"cart.php?action=add&id=$product_id\">Add To Cart</a></td>";

//echo "<td>" . '<a href="AmendProduct.php?id='. $row['ID'].'">Amend</a>' . "</td>";

//echo "<td>" . '<a href="DeleteProduct.php?id='. $row['ID'].'">Delete</a>' . "</td>";

echo "</tr>";

}

print "</table>";

}


?>
</div>
</div>
</div>
</div>
</div>
</div>

</div>

</div><!-- /#content-wrapper -->

<!-- footer row -->




            </div>

    <script src="js/jquery-latest.js"></script>
    <script src="js/bootstrap.js"></script>
</body>
    </html>
cart.php

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="PHP Shopping Cart Using Sessions" /> 
<meta name="keywords" content="shopping cart tutorial, shopping cart, php, sessions" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" media="all" href="/style/style.css" type="text/css" />
<title>Cart</title>


<?php
include 'connected.php';
?>


</head>
<body>


<?php

$ID = $_GET[ID];     //the product id from the URL 
$action     = $_GET[action]; //the action from the URL 

//if there is an product_id and that product_id doesn't exist display an error         message
if($ID && !productExists($ID)) {
    die("Error. Product Doesn't Exist");
}

switch($action) {   //decide what to do 

    case "add":
        $_SESSION['cart'][$ID]++; //add one to the quantity of the product     with id $product_id 
    break;

    case "remove":
        $_SESSION['cart'][$ID]--; //remove one from the quantity of the     product with id $product_id 
        if($_SESSION['cart'][$ID] == 0) unset($_SESSION['cart'][$ID]); //if     the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will     show zero, then -1, -2 etc when the user keeps removing items. 
    break;

    case "empty":
        unset($_SESSION['cart']); //unset the whole cart, i.e. empty the     cart. 
    break;

}

?>


<?php   

if($_SESSION['cart']) { //if the cart isn't empty
    //show the cart

    echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format     the cart using a HTML table

        //iterate through the cart, the $product_id is the key and     $quantity is the value
        foreach($_SESSION['cart'] as $ID => $quantity) {    

            //get the name, description and price from the database -     this will depend on your database implementation.
            //use sprintf to make sure that $product_id is inserted     into the query as a number - to prevent SQL injection
            $query = sprintf("SELECT  ProductName,  ProductPrice, FROM     Produts WHERE ID = %d;",
                            $ID); 

            $result = mysqli_query($connection, $query);

            //Only display the row if there is a product (though there     should always be as we have already checked)
            if(mysqli_num_rows($result) > 0) {

                list($ProductName, $ProductPrice) =     mysqli_fetch_row($result);

                $line_cost = $ProductPrice * $quantity;         //work out the line cost
                $total = $total + $line_cost;               //add to the total cost

                echo "<tr>";
                    //show this information in table cells
                    echo "<td align=\"center\">$name</td>";
                    //along with a 'remove' link next to the     quantity - which links to this page, but with an action of remove, and the id of the current product
                    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>";

            } else 
                echo "No results";

        }

        //show the total
        echo "<tr>";
            echo "<td colspan=\"2\" align=\"right\">Total</td>";
            echo "<td align=\"right\">$total</td>";
        echo "</tr>";

        //show the empty cart link - which links to this page, but with an     action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
        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{
    //otherwise tell the user they have no items in their cart
    echo "You have no items in your shopping cart.";

}

//function to check if a product exists
function productExists($ID) {
        //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
        $sql = sprintf("SELECT * FROM Products WHERE id = %d;",
                        $ID); 

        return mysqli_num_rows(mysqli_query($sql)) > 0;
}
?>

<a href="shop.php">Continue Shopping</a>

当我找到要添加到购物车的项目时,我会单击“添加到购物车”,然后它会显示我在其中选择的项目的购物车,但是当我单击“添加到购物车”时,它会显示购物车,但没有我在其中选择的项目

我认为shop.php中的第115行和第148行应该是

115: echo "<td><a href=\"cart.php?action=add&id=".$row['ID']."\">Add To Cart</a></td>";
148: echo "<td><a href=\"cart.php?action=add&id=".$row['ID']."\">Add To Cart</a></td>";

有几件事需要检查

确保“产品”表中的列名为“ID”,而不是“ID” 很少有大写的列名

其次,我不认为cart.php会获取您的id,因为

cart.php?action=add&id=$ID
使用小写字母“id”和

$ID = $_GET[ID];     //the product id from the URL 
$action     = $_GET[action]; //the action from the URL 
用户大写“ID”。因此,请改为:

$ID = $_GET["id"];
$action = $_GET["action"];
在cart.php上回显$ID和$action,以确保它们是从url中提取的

希望这对利兹同学有所帮助

编辑: 查看CygnusH33L答案,确保您到cart.php的链接已更改,以便使用$row['ID']。 或者定义$ID

<a href=\"cart.php?action=add&id=".$row['ID']."\">
or
$ID = $row['ID'];

欢呼这已经解决了一个问题,当我在项目上悬停时,它选择了正确的id,但在Cart中仍然没有出现任何问题。已将它们全部更改为大写id并尝试了回显。它确实拾取了正确的产品,但仍然没有将其放入Cart。这可能是因为您正在尝试在项目启动时增加未定义的会话第一次添加到购物车。我还会做一个打印会话['cart'];看看里面到底储存了什么。切换后再执行此操作。
<a href=\"cart.php?action=add&id=".$row['ID']."\">
or
$ID = $row['ID'];