只有";数组";word在尝试保存php购物车会话内容时被插入,无法检索购物车的内容
我有一个购物车,其中的订单正在显示,我使用会话来存储购物车的内容。现在我想做的是在按下checkout按钮时将购物车内容插入数据库。但每次任何用户签出时,只有单词“Array”被插入到数据库中。我试过的-只有";数组";word在尝试保存php购物车会话内容时被插入,无法检索购物车的内容,php,mysql,session,cart,Php,Mysql,Session,Cart,我有一个购物车,其中的订单正在显示,我使用会话来存储购物车的内容。现在我想做的是在按下checkout按钮时将购物车内容插入数据库。但每次任何用户签出时,只有单词“Array”被插入到数据库中。我试过的- $sqlimp = implode(",", $_SESSION["cart"] ); n打印$sqlimp,显示数组、数组、ArayArray、数组、数组(如果有两个项目)。以下是我的密码- index.php <?php session_start(); //
$sqlimp = implode(",", $_SESSION["cart"] );
n打印$sqlimp,显示数组、数组、ArayArray、数组、数组(如果有两个项目)。以下是我的密码-
index.php
<?php
session_start();
// print_r($_SESSION["user"]);
if(! isset($_SESSION["user"])){
header("Location: index.php");
}
require("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 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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="stylesheet" href="css/reset.css" /> -->
<link rel="stylesheet" href="styles.css" />
<title></title>
</head>
<body>
<h1> Welcome to our site! </h1>
<a href="logout.php" style="float:right">Logout</a>
<?php
// Echo session variables that were set on previous page
echo "Welcome " . $_SESSION["user"] . ".<br>";
?>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end of main-->
<div id="sidebar">
<h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
// $sql1= "INSERT INTO cart (contents) VALUES ('" . $_SESSION["cart"]. "')";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
// $query1= mysql_query($sql1);
while($row=mysql_fetch_array($query)){
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
<a href="home.php?page=cart">Go to cart</a>
<?php
}else{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end of sidebar-->
</div><!--end container-->
</body>
</html>
欢迎来到我们的网站!
运货马车
在checkout.php中的foreach循环中,您试图插入$\u会话['cart']
,而不是计算的$sqlimp
。因此,您需要将该行更改为:
$sql1= "INSERT INTO cart (contents) VALUES ('$sqlimp')";
你的代码有很多错误。我强烈建议您使用最新的手册学习php。您正在使用一个不推荐使用的函数进行mysql查询,您没有转义数据,等等。。。当你准备用这段代码启动一个网店时,你会遇到很多麻烦,比如订单失败,mysql注入等等。。。
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['cart'][$key]);
}else{
$_SESSION['cart'][$key]['quantity']=$val;
}
}
}
?>
<h1>View cart</h1>
<a href="home.php?page=products">Go back to the products page.</a>
<form method="post" action="home.php?page=cart">
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Items Price</th>
</tr>
<?php
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
$totalprice+=$subtotal;
?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>" /></td>
<td><?php echo $row['price'] ?>$</td>
<td><?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?>$</td>
</tr>
<?php
}
?>
<tr>
<td colspan="4">Total Price: <?php echo $totalprice ?></td>
</tr>
</table>
<br />
<button type="submit" name="submit">Update Cart</button>
<a href="checkout.php">Checkout</a>
</form>
<br />
<p>To remove an item set its quantity to 0. </p>
<?php
session_start();
include("connection.php");
$sql="SELECT * FROM products WHERE id_product IN (";
foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
$sqlimp = implode(",",$_SESSION['cart'] );
print_r($sqlimp);
$sql1= "INSERT INTO cart (contents) VALUES ('" . $_SESSION["cart"]. "')";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$query1= mysql_query($sql1);
// or die("Query to store cart failed");
?>
$sql1= "INSERT INTO cart (contents) VALUES ('$sqlimp')";