购物车不会拿起产品ID

时间:2014-05-02 13:08:35

标签: 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" />
    <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>

这个页面应该当我找到一个项目,我想添加到购物车,我会点击添加到购物车,然后它带出我在其中选择的项目的购物车然而当我点击添加到购物车,它带来购物车,但没有我在其中选择的项目。

2 个答案:

答案 0 :(得分:0)

我认为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>";

答案 1 :(得分:0)

要检查的几件事。

确保“商品”表格中的列名称为“ID”,而不是“ID” (很少有大写的列名。)

$row['ID'] -> $ row['id']

其次我不认为cart.php正在提升你的身份因为。

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,以确保它们从网址中被提取。

希望这有帮助,利兹大都会学生。




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

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