PHP,购物车程序出错
PHP, Error in Shopping Cart Program
我正在尝试在 PHP 中创建购物车。它运作良好。但后来我从购物车中删除了所有商品,但失败了。我认为它与 session 有关系,但我不完全确定。
我收到此错误:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Apache24\htdocs\phpshop\index.php on line 85
Call Stack
# Time Memory Function Location
1 0.0002 244096 {main}( ) ...\index.php:0
2 0.0017 255352 mysqli_fetch_array ( )
I used this Shopping cart tutorial to build this program.
cart.php代码:
<?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="index.php?page=products">Go back to products page</a>
<form method="post" action="index.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";
第 85 行关于此处 ---> $query = mysqli_query($con,$sql);
$totalprice=0;
$numrows = mysqli_num_rows($query);
if($numrows == 0)
{
header("Location: index.php");
}
while($row=mysqli_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>
</form>
<br />
<p>To remove an item, set it's quantity to 0. </p>
index.php
<?php
session_start();
require("includes/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 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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="css/style.css" />
<title>Shopping cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end 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.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($query))
{
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
<a href="index.php?page=cart">Go to cart</a>
<?php
}
else
{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end sidebar-->
</div><!--end container-->
</body>
</html>
products.php
<?php
if(isset($_GET['action']) && $_GET['action']=="add")
{
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}
else
{
$sql_s="SELECT * FROM products WHERE id_product={$id}";
$query_s=mysqli_query($con,$sql_s);
if(mysqli_num_rows($query_s)!=0)
{
$row_s=mysqli_fetch_array($query_s);
$_SESSION['cart'][$row_s['id_product']]=array
(
"quantity" => 1,
"price" => $row_s['price']
);
}
else
{
$message="This product id it's invalid!";
}
}
}
?>
<h1>Product List</h1>
<?php
if(isset($message)){
echo "<h2>$message</h2>";
}
?>
<table>
<?php
$sql = "SELECT * FROM products ORDER BY name ASC";
$query = mysqli_query($con,$sql);
while ($row=mysqli_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>
你的作业
$query = mysqli_query($con,$sql);
失败并返回布尔值 FALSE。这是因为 $con
设置不正确。
http://php.net/manual/en/mysqli.query.php
因此,您将 FALSE 作为 $query
传递给 mysqli_fetch_array
while($row=mysqli_fetch_array($query))
这是导致您报告的错误的原因
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,第 85 行 C:\Apache24\htdocs\phpshop\index.php 中给出的布尔值
我正在尝试在 PHP 中创建购物车。它运作良好。但后来我从购物车中删除了所有商品,但失败了。我认为它与 session 有关系,但我不完全确定。 我收到此错误:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Apache24\htdocs\phpshop\index.php on line 85
Call Stack
# Time Memory Function Location
1 0.0002 244096 {main}( ) ...\index.php:0
2 0.0017 255352 mysqli_fetch_array ( )
I used this Shopping cart tutorial to build this program.
cart.php代码:
<?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="index.php?page=products">Go back to products page</a>
<form method="post" action="index.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";
第 85 行关于此处 ---> $query = mysqli_query($con,$sql);
$totalprice=0;
$numrows = mysqli_num_rows($query);
if($numrows == 0)
{
header("Location: index.php");
}
while($row=mysqli_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>
</form>
<br />
<p>To remove an item, set it's quantity to 0. </p>
index.php
<?php
session_start();
require("includes/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 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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="css/style.css" />
<title>Shopping cart</title>
</head>
<body>
<div id="container">
<div id="main">
<?php require($_page.".php"); ?>
</div><!--end 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.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query = mysqli_query($con,$sql);
while($row=mysqli_fetch_array($query))
{
?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php
}
?>
<hr />
<a href="index.php?page=cart">Go to cart</a>
<?php
}
else
{
echo "<p>Your Cart is empty. Please add some products.</p>";
}
?>
</div><!--end sidebar-->
</div><!--end container-->
</body>
</html>
products.php
<?php
if(isset($_GET['action']) && $_GET['action']=="add")
{
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}
else
{
$sql_s="SELECT * FROM products WHERE id_product={$id}";
$query_s=mysqli_query($con,$sql_s);
if(mysqli_num_rows($query_s)!=0)
{
$row_s=mysqli_fetch_array($query_s);
$_SESSION['cart'][$row_s['id_product']]=array
(
"quantity" => 1,
"price" => $row_s['price']
);
}
else
{
$message="This product id it's invalid!";
}
}
}
?>
<h1>Product List</h1>
<?php
if(isset($message)){
echo "<h2>$message</h2>";
}
?>
<table>
<?php
$sql = "SELECT * FROM products ORDER BY name ASC";
$query = mysqli_query($con,$sql);
while ($row=mysqli_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>
你的作业
$query = mysqli_query($con,$sql);
失败并返回布尔值 FALSE。这是因为 $con
设置不正确。
http://php.net/manual/en/mysqli.query.php
因此,您将 FALSE 作为 $query
传递给 mysqli_fetch_arraywhile($row=mysqli_fetch_array($query))
这是导致您报告的错误的原因
警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,第 85 行 C:\Apache24\htdocs\phpshop\index.php 中给出的布尔值