Jump to content
Larry Ullman's Book Forums

Recommended Posts

I have worked through chapter 17 and some sessions are carrying through to further pages, but some not. They are going from this page:

 

 

<?php // Start output buffering:

ob_start();

session_start();

?>

 

<?php #script together. This combines addtocart and view cart.

$_SESSION['total'] = $_POST['total'];

//print_r($_SESSION);

$page_title = 'Add to cart';

if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) {

$pid = (int) $_GET['pid'];

if (isset($_SESSION['cart'][$pid])) {

$_SESSION['cart'][$pid]['quantity']++;

//echo '<p>Another copy of product has been added to shopping basket</p>';

}else{ //new product

require_once ('../mysqli_connect.php');

$q="SELECT price FROM product WHERE product_id=$pid";

$r=mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) {

list($price) = mysqli_fetch_array ($r, MYSQLI_NUM);

$_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price);

// echo '<p>The print has been added</p>';

}else{

echo '<div align="center">Your basket has been updated</div>';

}

//mysqli_close($dbc);

}

}else{ //no print ID

echo '<div align="center"> Your basket has been updated</div>';

}

# Script 17.9 - view_cart.php.

// Check if the form has been submitted (to update the cart):

if (isset($_POST['submitted'])) {

 

// Change any quantities:

foreach ($_POST['qty'] as $k => $v) {

 

// Must be integers!

$pid = (int) $k;

$qty = (int) $v;

 

if ($qty ==0) { // Delete.

unset ($_SESSION['cart'][$pid]);

} elseif ($qty >0 ) { // Change quantity.

$_SESSION['cart'][$pid]['quantity'] = $qty;

}

 

} // End of FOREACH.

} // End of SUBMITTED IF.

 

// Display the cart if it's not empty...

if (!empty($_SESSION['cart'])) {

 

// Retrieve all of the information for the prints in the cart:

require_once ('../mysqli_connect.php');

$q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN (";

foreach ($_SESSION['cart'] as $pid => $value) {

$q .= $pid . ',';

}

$q=substr($q, 0, -1) . ') ORDER BY product_name ASC';

$r = mysqli_query ($dbc, $q);

 

// Create a form and a table:

echo '<form action="basket.php" method="post">

<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">

<tr>

<td align="left" width="30%"><b>Description</b></td>

<td align="left" width="30%"><b>Product Name</b></td>

<td align="right" width="10%"><b>Price</b></td>

<td align="center" width="10%"><b>Qty</b></td>

<td align="right" width="10%"><b>Total Price</b></td>

</tr>

';

 

// Print each item...

$total =0; // Total cost of the order.

while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {

 

// Calculate the total and sub-totals.

$subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price'];

$postage=1.99;

$total += ($postage + $subtotal);

 

// Print the row.

echo "\t<tr>

<td align=\"left\">{$row['description']}</td>

<td align=\"left\">{$row['product_name']}</td>

<td align=\"right\">£{$_SESSION['cart'][$row['product_id']]['price']}</td>

<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td>

<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>

</tr>\n";

 

} // End of the WHILE loop.

$_SESSION['total']=$total;

$_SESSION['product_name']=$pn;

$_SESSION['order_id'] =$order_id;

 

mysqli_close($dbc); // Close the database connection.

 

 

// Print the footer, close the table, and the form.

echo '<tr>

 

<td colspan="4" align="right"><b> Postage:</b></td><br/>

<td align="right">£' . number_format ($postage, 2) . '</td>

<br/>

<br/>

<br/>

<br/>

<td colspan="4" align="right"><b>Total:</b></td>

<td align="right">£' . number_format ($total, 2) . '</td>

</tr>

</table><p align="left">Enter a quantity of 0 to remove an item.

<div align="left"><input type="submit" name="submit" value="Update" /></div>

<input type="hidden" name="submitted" value="TRUE" />

<p align="center"><a href="../logincustomer.php"><img src="checkout.gif" width="83" height="27" border="0"/></a></p>';

 

} else {

echo '<p>Your cart is currently empty.</p>';

}

?>

 

TO THIS PAGE.

session_start();

if (isset($_POST['submitted'])) {

require_once (MYSQL);

// Trim all the incoming data:

$trimmed = array_map('trim', $_POST);

 

// Check for an email address:

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {

$e = mysqli_real_escape_string ($dbc, $trimmed['email']);

} else {

$e = FALSE;

echo '<p class="error">Please enter a valid email address!</p>';

}

if (!empty($_POST['password'])) {

$p = mysqli_real_escape_string ($dbc, $_POST['password']);

} else {

$p = FALSE;

echo '<p class="error">You forgot to enter your password!</p>';

}

if ($e && $p) { // If everything's OK.

// Query the database:

$q = "SELECT customer_id, firstname FROM customers WHERE (email='$e' AND password=SHA1('$p') )";

$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (@mysqli_num_rows($r) == 1) { // A match was made.

 

$row = mysqli_fetch_array ($r, MYSQLI_ASSOC);

$_SESSION['customer_id'] = $row['customer_id'];

 

$me = $_SESSION['customer_id'];

$total = $_SESSION['total'];

$pn = $_SESSION['product_name'];

$order_id = $_SESSION['order_id'];

$pn = $_SESSION['product_name'];

 

$body = "'$order_id' \nThe total of your order is '$total'\n Your customer number is '$me'\nPlease quote your customer number if you need to contact us";

 

The information in the $body is what I am looking at '$total' and '$me' sends in the email, but nothing else. I cannot get the product name or order id to show. Any ideas what the problem is?

Link to comment
Share on other sites

Hi Kerry,

 

Please can you use the code tags when you post code else it makes it hard work reading.

 

The issue is here:

 

$_SESSION['order_id'] =$order_id;

 

That's the first time $order_id is used on that page meaning it doesn't have a value.

 

Make sure you have error reporting turned on which would highlight trying to assign a variable that does not exist.

 

Hope that helps.

  • Upvote 1
Link to comment
Share on other sites

Stuart, thanks for replying. Sorry about the code tags, was my first post.

 

I have used product_name earlier in the code, but this does not allow me to output this in the email either. The only two that it allows me to show is the total and customer_id. I have the same problem with quantity and description.

 

Any ideas on how I can bring accross the product_name as with the customer_id and total?

Thanks

Kerry

 

$me = $_SESSION['customer_id'];

$total = $_SESSION['total'];

$pn = $_SESSION['product_name'];

 

$body = "'$pn'\nThe total of your order is '$total'\n Your customer number is '$me'\nPlease quote your customer number if you need to contact us";

 

 

$_SESSION['product_name'];

Link to comment
Share on other sites

 Share

×
×
  • Create New...