Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Undefined variable error

    Hi,
    I've just bought the book PHP and MYSQL Web Development and have started to work through the examples. I'm running Linux Mint 14 with an Apache server. I installed PHP and MYSQL using Apache Friends. The server is started and simple PHP programs are working correctly. Code is saved to a 'tutorials' folder in apache htdocs using eclipse with the php plug-in.
    I started a new php project in eclipse and added two files to the project. the first is an html file which I have called index.html. This is the code:

    Code:
    <html>
    <head><title>Bob's Auto Parts</title></head>
    <body>
    <form action="processorder.php" method="post">
    <table border="0">
    <tr bgcolor="#cccccc">
      <td width="150">Item</td>
      <td width="15">Quantity</td>
    </tr>
    <tr>
      <td>Tires</td>
      <td align="center"><input type="text" name="tireqty" size="3"
         maxlength="3" /></td>
    </tr>
    <tr>
      <td>Oil</td>
      <td align="center"><input type="text" name="oilqty" size="3" 
         maxlength="3" /></td>
    </tr>
    <tr>
      <td>Spark Plugs</td>
      <td align="center"><input type="text" name="sparkqty" size="3"
         maxlength="3" /></td>
    </tr>
    <tr>
      <td>How did you find Bob's?</td>
      <td><select name="find">
            <option value = "a">I'm a regular customer</option>
            <option value = "b">TV advertising</option>
            <option value = "c">Phone directory</option>
            <option value = "d">Word of mouth</option>
          </select>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="submit" value="Submit Order" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Variable such as tireqty and oilqty are defined. The second file named processorder (defined as a form action in the html file) contains this code.

    Code:
    <html>
    <head>
      <title>Bob's Auto Parts - Order Results</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Order Results</h2>
    <?php
      echo '<p>Order processed at ';
      echo date('H:i, jS F');
      echo '</p>';
      echo '<p>Your order is as follows: </p>';
      echo $tireqty.' tires<br />';
      echo $oilqty.' bottles of oil<br />';
      echo $sparkqty.' spark plugs<br />';
    
      $totalqty = 0;
      $totalamount = 0.00;
    
      $totalqty = 0;
      $totalqty = $tireqty + $oilqty + $sparkqty;
      echo 'Items ordered: '.$totalqty.'<br />';
    
      $totalamount = 0.00;
    
      define('TIREPRICE', 100);
      define('OILPRICE', 10);
      define('SPARKPRICE', 4);
    
      $totalamount = $tireqty * TIREPRICE
                   + $oilqty * OILPRICE
                   + $sparkqty * SPARKPRICE;
    
      echo 'Subtotal: $'.number_format($totalamount,2).'<br />';
    
      $taxrate = 0.10;  // local sales tax is 10%
      $totalamount = $totalamount * (1 + $taxrate);
      echo 'Total including tax: $'.number_format($totalamount,2).'<br />';
    
    ?>
    </body>
    </html>
    These examples are copied direclty from the book.
    Line 5 in the html code
    Code:
    <table border="0">
    is generating a warning in eclipse and i get the following error when i run localhost/tutorials. Here is the error generated:

    Code:
    Bob's Auto Parts
    
    Order Results
    
    Order processed at 22:38, 18th January
    
    Your order is as follows:
    
    
    Notice: Undefined variable: tireqty in /opt/lampp/htdocs/tutorials/processorder.php on line 13
    tires
    
    Notice: Undefined variable: oilqty in /opt/lampp/htdocs/tutorials/processorder.php on line 14
    bottles of oil
    
    Notice: Undefined variable: sparkqty in /opt/lampp/htdocs/tutorials/processorder.php on line 15
    spark plugs
    
    Notice: Undefined variable: tireqty in /opt/lampp/htdocs/tutorials/processorder.php on line 21
    
    Notice: Undefined variable: oilqty in /opt/lampp/htdocs/tutorials/processorder.php on line 21
    
    Notice: Undefined variable: sparkqty in /opt/lampp/htdocs/tutorials/processorder.php on line 21
    Items ordered: 0
    
    Notice: Undefined variable: tireqty in /opt/lampp/htdocs/tutorials/processorder.php on line 31
    
    Notice: Undefined variable: oilqty in /opt/lampp/htdocs/tutorials/processorder.php on line 32
    
    Notice: Undefined variable: sparkqty in /opt/lampp/htdocs/tutorials/processorder.php on line 32
    Subtotal: $0.00
    Total including tax: $0.00
    Can anyone help with these errors. Code is exactly as listed in the book.

    Thanks,

    Tim

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You appear to be missing a chunk . Re-examine the section of the book but, otherwise, you might need to search the books' website for errata.

    Code like the following is missing:

    PHP Code:
    if (isset($_POST['tireqty']) && !empty($_POST['tireqty'])) {
        
    $tireqty $_POST['tireqty'];

    The warning for table is probably that it lacks the summary attribute, but this is no longer a requirement. and the attribute may even be dropped by W3C.

    Code:
    <tr bgcolor="#cccccc">
    BTW This bgcolor attribute is obsolete, and your pages should have a DOCTYPE declaration - but we can blame the book for this!
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    tm2383 (01-19-2013)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Answer: toss your book. Its too old to work with (now 4.5 years old).
    The error on an html element in eclipse is likely the use of deprecated HTML (border in table). Use CSS.
    The problem with the PHP is it expects you have register_globals enabled. This directive has been disabled by default since 4.2, deprecated in 5.3, and removed in 5.4. As pointed out, all of your data must be extracted from a superglobal, which in this case is $_POST. You can however use isset() to check multiple conditions by giving all the arguments you expect, then within that block extract all the variables and process per normal.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    tm2383 (01-19-2013)

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    @Fou-Lu. I'm so up-to-date that I didn't even know that register_globals existed

    I recommend PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide by Larry Ullman (Sept 2011 - recent enough!).
    Last edited by AndrewGSW; 01-19-2013 at 07:40 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the replies everyone. The problem seems to have been that the script on the CD had missing code from it. By following the example in the book itself, I added the following code and got it to work:
    Code:
    $tireqty = $_POST ['tireqty'];
     $oilqty = $_POST ['oilqty']; 
     $sparkqty = $_POST ['sparkqty'];
    Later in the book, better methods are discussed in the regex section.

    Register_globals is discussed, although the book says never to use it due to security risks. The book uses 5.3 when it was still available.

    The mysql API database connector used in the book is mysqli .

    Will this book still do for the basics, do you think?

    Thanks

    Tim

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Will this book still do for the basics, do you think?
    Yes, should be okay. I would supplement with reference to the online docs. Read the relevant pages as you go along; get to know the docs very well, particularly the comments.

    The book should cover prepared statements. This is an important subject.

    This site looks very good as well; although I've only come across it today.

    PS Remove the space(s) $_POST ['tireqty'];
    Last edited by AndrewGSW; 01-19-2013 at 11:17 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    tm2383 (01-19-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •