以下代码不是我自己的代码。我正在努力学习如何制作购物车,但我仍然坚持使用本教程的这一部分。请验证代码并告诉我我做错了什么。
这是products.php:
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$query_s=mysql_query($sql_s);
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row_s'id_product']]=array(
"quantity" => 1,
"price" => $row_s['price']
);
}
}
?>
<h1>Product List</h1>
<?php
echo print_r($_SESSION['cart']);
?>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$sql = "SELECT * FROM products ORDER BY name ASC";
$query = mysql_query($sql);
while($row=mysql_fetch_array($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['id_product']>">Add to cart</a></td>
</tr>
<?php
}
?>
</table>
这是index.php:
<?php
session_start();
include("connection.php");
if(isset($_GET['page']))
{
$pages=array("products","cart");
if(in_array($_GET['page'], $pages)) {
$_page=$_GET['page'];
}else{
$_page="products";
}
}
else {
$_page="products";
}
?>
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Shopping cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div> <!--end of main-->
<div id="sidebar">
</div> <!--end of sidebar-->
</div> <!--end of container-->
</body>
</html>
这是css文件style.css:
a {
color: #48577D;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
body {
font-family: Verdana;
font-size: 12px;
color: #444;
}
h1, h2 { margin-bottom: 15px;}
h1 { font-size:18px;}
h2 {font-size: 16px;}
#container {
width:700px;
margin: 150px auto;
background-color:#eee;
padding:15px;
overflow:hidden;
}
#main {
width: 490px;
float: left;
}
#main table {
width: 480px;
}
#main table th {
padding: 10px;
background-color: #48577D;
color: #fff;
text-align: left;
}
#main table td {
padding: 5px;
}
#sidebar {
width: 200px;
float: left;
}
这是connection.php:
<?php
$con=mysql_connect("localhost","root","");
$database=mysql_select_db('tutorials');
?>
这是cart.php:
This is your cart
它没有正确递增值。感谢。
答案 0 :(得分:0)
这里有语法错误:
$_SESSION['cart'][$row_s'id_product']]
我认为它应该是:
$_SESSION['cart'][$row_s['id_product']]
如果启用错误报告,您将轻松捕获这些错误。没有理由不在开发中打开它。