Jump to content

Cart Page Not Working!


juxmaichong

Recommended Posts

I got my product page to display correctly but when you try to add products to it, it goes to a cart page called shop.php and no items are displayed!

Also, I get this error message, "Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/mlee/public_html/Project/order.php on line 5 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/mlee/public_html/Project/order.php on line 5 Error. Product Doesn't Exist."

I tried putting this function as an include file and it gives the same error message.

I just need all the addeds items from one session to be displayed and then be provided with the total and saved the order to database.

 

shop page ( Should works just like a cart page)

<?php
session_start();
function productExists($product_id) {
$sql = sprintf("SELECT * FROM bakery_product WHERE id = %d;", $product_id);
return mysqli_num_rows(mysqli_query($sql)) > 0;
}


error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once('../../mysqli_connect.php');


$product_id = $_GET['id'];
$action = $_GET['action'];

if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}


switch($action) {
case "add":
$_SESSION['cart']['$product_id']++;
break;

case "remove":
$_SESSION['cart']['$product_id']--;
if($_SESSION['cart']['$product_id'] == 0) unset($_SESSION['cart']['$product_id']);
break;

case "empty":
unset($_SESSION['cart']);
break;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Order</title>
<link rel='shortcut icon' href='favicon.ico' type='image/x-icon'/ >
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header-wrapper">
<div id="header">
<div id="logo">
<p><img src="images/bakerylogo.png" width="268" height="198" alt="logo" /></p>
<p> </p>
</div>
</div>
</div>
<!-- end #header -->
<?php include ('include/header.html');?>
<!-- end #menu -->
<div id="page">
<div id="page-bgtop">
<div id="page-bgbtm">
<div id="content">
<div class="post">
<div class="entry">
<?php
if($_SESSION['cart']) {
echo "<table border=\"1\" padding=\"3\" width=\"70%\">";
foreach($_SESSION['cart'] as $product_id => $quantity) {


$sql = sprintf("SELECT product_decription, price, category FROM bakery_product WHERE p_id = %d;",
$product_id);


$result = mysqli_query($dbc,$sql)
or die ('Error quering database.');


if(mysqli_num_rows($result) > 0) {
echo 'more than 0';
list($product_decription, $price, $category) = mysqli_fetch_row($result);


$cost = $price * $quantity;
$total = $total + $cost;


echo "<tr>";
echo "<td align=\"center\">$product_decription</td>";
echo "<td align=\"center\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>";
echo "<td align=\"center\">$cost</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\">Total</td>";
echo "<td align=\"right\">$total</td>";
echo "</tr>";


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{
echo "You have not preorder anything.";
}
?>

</div>
</div>
<div style="clear: both;"> </div>
</div>
<!-- end #content -->
<?php include ('include/sidermenu.html');?>
<!-- end #sidebar -->
<div style="clear: both;"> </div>
</div>
</div>
</div>
<!-- end #page -->
</div>
<?php include ('include/footer.html');?>

Link to comment
Share on other sites

The mysqli_query() function requires the connection as a parameter. Which is what the error message is telling you. You've done it correctly later in the script, you will need to pass the connection as a parameter to the function and then use it in the function

 

THanks for your reponse. But if I understood your response, I put it like this:

function productExists($product_id) {
$dbc = mysqli_connect('', ', '', ')
   $sql = sprintf("SELECT * FROM bakery_product WHERE id = %d;", $product_id); 
   $result = mysqli_query($dbc, $sql);
return mysqli_num_rows ($result) > 0;
}

and it still gives me the same error. How am I coding this wrong? Can you give me an example for this code this correctly? I would appreciate it very much as I am still learning and some of this is still new to me.

Link to comment
Share on other sites

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/mlee/public_html/Project/order.php on line 5

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/mlee/public_html/Project/order.php on line 5

 

Error. Product Doesn't Exist.

 

Are you sure it is the same error message? The message from your original quote was actually two error messages from mysqli and one from your application.

 

You should not be getting the first error message since you are now providing two parameters. Post the actual error message that you received.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.