Wrox Programmer Forums
|
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2004, 03:53 AM
Registered User
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default CH. 14 cart.php code problem

Yes, I did read the previous Post regarding checkout3.php, but cart.php from chapter 14 is coming up "Undefined variable total at line 83"...which would be this line in my text editor:

//add extended price to total
$total=$extprice + $total;

Just as a reference here is the cart.php script:(I have omitted the Image lines of code as my cart doesn't currently show images. Also session_autostart is curently switched to on in my php.ini)

Code:
<?php
session_id();

$connect=mysql_connect("localhost", "username", "password")
    or die ("Check your server connection loser!");
mysql_select_db("ecommerce");

?>

<HTML>
<HEAD>
<TITLE>Contents of Your Cart</TITLE>
<LINK href="/css/shell.css" type="text/css" rel="stylesheet">
</HEAD>

<BODY>

<div align="center">
<h1>You currently have...</H1>

<?php
$sessid=session_id();

$query="select * from carttemp where sess='$sessid'";
$results=mysql_query($query)
    or die (mysql_error());
$rows=mysql_num_rows($results);
echo $rows;

?>

products in your cart<br>

<table class="shop" align="center" cellpadding="5">

<TR>
    <TD>Quantity</TD>
    <TD>Item Name</TD>
    <TD>Price per Package</TD>
    <TD>Extended Price</TD>
    <TD></TD>
    <TD></TD>
<TR>
<?php

while ($row=mysql_fetch_array($results)) {
    extract ($row);

    $prod="SELECT * FROM products WHERE prodnum='$prodnum'";
    $prod2=mysql_query($prod);
    $prod3=mysql_fetch_array($prod2);
    extract($prod3);

echo "<td><form method='post' action='change.php'>
    <input type='hidden' name='prodnum' value='$prodnum'>
    <input type='hidden' name='sessid' value='$sessid'>
    <input type='hidden' name='hidden' value='$hidden'>
    <input type='text' name='qty' size='2' value='$quan'>";
echo "</td>";
echo "<td>";
echo "<a href='getprod.php?prodid=" .$prodnum ."'>";
echo $name;
echo "</td></a>";
echo "<td align='right'>";
echo $price;
echo "</td>";
echo "<td align='right'>";
$extprice=number_format($price * $quan, 2);
echo $extprice;
echo "</td>";
echo "<td>";
echo "<input type='submit' name='submit' value='Change Qty' class='send'></form></td>";
echo "<td>";
echo "<form method='POST' action='delete.php'>
    <input type='hidden' name='prodnum' value='$prodnum'>
    <input type='hidden' name='qty' value='$quan'>
    <input type='hidden' name='hidden' value='$sessid'>";

echo "<input type='submit' name='submit' value='Delete Item' class='send'>
</form></td>";
echo "</tr>";

$total=$extprice + $total;

}

?>

<tr>
<td colspan="4" align="right">Your Total Before Shipping:</td>
<td align="right"><?php echo number_format($total, 2) ?> </td>
<td></td>
<td></td>
</tr>
</table>

<form method="POST" action="checkout.php">
<input type="submit" name="submit" value="Proceed to Checkout">
</form>

<a href="/shop/shop_index.php">Back to the Main Page</a>
</div>
</BODY>
</HTML>
Any remedies?

Sincerely,
shawnrberg
 
Old June 18th, 2004, 09:23 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This puzzles me. Why is it that I never get any error about having undefined variables?? Everyone else seem to get it a lot of them.

Christian

 
Old June 21st, 2004, 05:44 PM
Authorized User
 
Join Date: May 2004
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It depends on the "error_reporting" setting in php.ini. czambran, you probably have yours set to E_ALL & ~E_NOTICE (which I believe is the default), which only shows fatal errors and warnings. Shawnrberg, my guess is that yours is set to E_ALL, which shows all errors, warnings, and notices.

There are two ways to remedy this -- 2 permanent solutions, and a temporary one. There are a couple of ways to do it temporarily.

One permanent solution is this: edit your php.ini file and find the line "error_reporting = E_ALL." Change it to "error_reporting = E_ALL & ~E_NOTICE." I do NOT recommend this. I almost always recommend to my clients that they set error_reporting to E_ALL. The reason is that even though most notices are non-fatal, they could be symptoms of larger problems. Good coding practice will help you to fix all problems, even notices, which brings us to the second permanent solution: Fix the notice by declaring your variables. In this particular case, a line that sets $total = "" will suffice to prevent the notice you got.

*Note: I realize the book does not always follow good coding practices. The book's job is to teach you basic concepts and techniques, and hopefully turn the reader into a decent PHP developer. It is not intended to turn you into a great coder, with good coding practices. If we tried to do that, much of the code would be lost on beginners, and the book would be 2000 pages long. However, whenever asked, I do like to encourage good coding practices.

Ok... the temporary solution (which I don't condone, but it will prevent the notice) is to use the function error_reporting() in your script:

error_reporting(E_ALL & ~E_NOTICE);

To suppress all errors (which I do NOT recommend):

error_reporting(0);

---------
Good coding practices take more time. But in the long run, they prevent more headaches and save you troubleshooting time.

For more information about error handling and logging, I recommend you take a look at this page:http://us3.php.net/manual/en/ref.errorfunc.php


Michael K. Glass
Author, Beginning PHP, Apache, MySQL Web Development





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch.14 Creating Windows Form User Controls problem SAIFI BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 1 September 29th, 2008 05:51 AM
Ch 14 AdNew.asp example - Acess permission problem pycockd BOOK: Beginning ASP 3.0 4 February 7th, 2006 06:36 PM
Ch. 13/14 creating own custom controls...problem.. Chimlim BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 August 10th, 2005 04:21 AM
Ch 10: Problem with shopping cart example code Storm BOOK: Beginning ASP.NET 1.0 0 October 11th, 2004 03:24 PM
PHP Source Code for Shopping cart haribabu PHP How-To 0 January 27th, 2004 02:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.