Hello all, I’ve been working on a shopping cart project for some fun and have hit a brick wall. I’m quite new to PHP and was wondering if anyone could help me resolve this issue. Basically, when the user clicks the checkout button everything works correctly, but the following error is given;
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\PizzaStore\index.php on line 86
Here is my code:
[code]<?php
include(“includes/header.php”);
?>
'; print_r($array); echo ''; } ?>
<?php if (isset($_POST['checkout'])) { if ($_SESSION['shopping_cart'] != "" && isset($_SESSION['shopping_cart'])) { foreach($_SESSION['shopping_cart'] as $item) { $id = $item["id"]; $quantity = $item["quantity"]; $updateStock = mysqli_query ($con, "UPDATE products SET stock = stock - $quantity WHERE id = '$id'"); if (mysqli_num_rows($updateStock) == 0) { unset($_SESSION['shopping_cart']); //header("Location: index.php"); } } } else { unset($_SESSION['shopping_cart']); //header("Location: index.php"); } } ?>
<?php
$query = 'SELECT * FROM products ORDER BY id ASC';
$result = mysqli_query ($con, $query);
if ($result):
if(mysqli_num_rows($result)>0):
while($product = mysqli_fetch_assoc($result)):
//print_r($product);
?>
<div class="col-sm-4 col-md-3">
<form method="POST" action="index.php?action=add&id=<?php echo $product['id']; ?>">
<div class="products">
<img src="<?php echo $product['image']; ?>" class="img-responsive" />
<h4 class="text-info"><?php echo $product['name']; ?></h4>
<h4>£<?php echo $product['price']; ?></h4>
<input type="text" name="quantity" class="form-control" value="1" />
<input type="hidden" name="name" value="<?php echo $product['name']; ?>" />
<input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
<input type="submit" name="add_to_cart" style="margin-top: 5px; " class="btn btn-info" value="Add to Cart" />
</div>
</form>
</div>
<?php
endwhile;
endif;
endif;
?>
<div style="clear:both"></div>
<br />
<div class="table-responsive">
<table class="table">
<h3>Order Details</h3></th></tr>
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Item Price(£)</th>
<th width="15%">Total(£)</th>
<th width="5%">Action</th>
</tr>
<?php
if(!empty($_SESSION['shopping_cart'])):
$total = 0;
foreach($_SESSION['shopping_cart'] as $key => $product):
?>
<tr>
<td><?php echo $product['name']; ?></td>
<td><?php echo $product['quantity']; ?></td>
<td>£<?php echo $product['price']; ?></td>
<td>£<?php echo number_format($product['quantity'] * $product['price'], 2); ?></td>
<td>
<a href="index.php?action=delete&id=<?php echo $product['id']; ?>">
<div class="btn-danger">Remove</div>
</a>
</td>
</tr>
<?php
$total = $total + ($product['quantity'] * $product['price']);
endforeach;
?>
<tr>
<td colspan="3" align="right">Cart Total:</td>
<td align="right">£<?php echo number_format($total, 2); ?> GBP</td>
<td></td>
</tr>
<tr>
<!-- Show checkout button only if the shopping cart is not empty -->
<td colspan="5">
<?php
if (isset($_SESSION['shopping_cart'])):
if (count($_SESSION['shopping_cart']) > 0):
?>
<div class="checkout">
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="submit" class="btn btn-info" name="checkout" value="Buy Now"/>
</form>
</div>
<?php endif; endif; ?>
</td>
</tr>
<?php
endif;
?>
</table>
</div>
Any help would be greatly appreciated. Thanks.