-2

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
PHP - Undefined variable

I am doing PHP for the first time, quite enjoying it however stuck on one error which is

Notice: Undefined variable: customerid in C:\xampp\htdocs\cravendale\showconfirm.php on line 32

Notice: Undefined variable: holidayid in C:\xampp\htdocs\cravendale\showconfirm.php on line 43

code for showconfirm.php

<?php
//Capture the customerid from the URL and send to a local variable
$booking = $_GET['customerid'];


//echo out some blurb
echo "<h2>Thank you for your booking</h2>
You may wish to print this page for future reference
<br />
<br />
<h2>Your details</h2>";

//Open up our dataconnection
include 'config.php';
include 'opendb.php';

//Get the booking details from the database

$getbooking = mysql_query("SELECT *
        FROM tblbookings
        WHERE tblbookings.bookingid = @$booking");
while($booking = mysql_fetch_array($getbooking))
        {
        @$customerid = $booking['customerid'];
        $holidayid = $booking['holidayid'];

        }
//Get the customer details

$getcustomer = mysql_query("SELECT *
        FROM tblcustomers
        WHERE tblcustomers.customerid = $customerid");

while($customer = mysql_fetch_array($getcustomer))
        {

        echo "<p><b>First Name:</b> " . $customer['customerfirstname'] . "<br /><br />
        <b>Last Name:</b> " . $customer['customerlastname']. "<br /><br /></p><h2>Your Holiday</h2>";
        }

$getholiday = mysql_query("SELECT *
        FROM tblholidays
        WHERE tblholidays.holidayid= $holidayid");

        while($myholidays = mysql_fetch_array($getholiday))
        {
        //We get the destination name
                $chosendestination = $myholidays['destinationid'];
                $getchosendestination = mysql_query("SELECT tbldestinations.destinationname
                FROM tbldestinations
                WHERE tbldestinations.destinationid = $chosendestination" );

                while($mydestination = mysql_fetch_array($getchosendestination))
                {
                echo
                "<b>Destination: </b>" . $mydestination['destinationname'];
                }
        //We get the name of the hotel
                $chosenhotel = $myholidays['hotelid'];
                $getchosenhotel = mysql_query("SELECT tblhotels.hotelname
                FROM tblhotels
                WHERE tblhotels.hotelid = $chosenhotel" );

                while($myhotel = mysql_fetch_array($getchosenhotel))
                {
                echo
                "<br /><br /><b>Hotel: </b>" . $myhotel['hotelname'];
                }

                //We get the price
                $chosenprice = $myholidays['pricebandid'];
                $getchosenprice = mysql_query("SELECT tblpricebands.pricebandcost
                FROM tblpricebands
                WHERE tblpricebands.pricebandid = $chosenprice" );

                while($myprice = mysql_fetch_array($getchosenprice))
                {
                echo
                "<br /><br /><b>Price: </b>&pound;" . $myprice['pricebandcost'];
                }

                    $phpdate3 = strtotime( $myholidays['holidaystartdate'] );
                    $mysqldate3 = date( 'd-m-Y', $phpdate3 );
        echo "

                <br /><br /><b>Start date: </b>" . $mysqldate3 ;        
        }
?>

i have researched about this error a lot, the closest clue i am getting is to put "@" before $customerid and $holidayid. The errors dissappear but the information from the form isn't loaded.

Any help would be greatly appreciated.

Community
  • 1
  • 1
zain zorro
  • 41
  • 1
  • 4
  • 11

3 Answers3

2

Your first query does obviously not return anything,

$getbooking = mysql_query("SELECT *
    FROM tblbookings
    WHERE tblbookings.bookingid = $booking");

therfore the following "while" loop does not even perform a single cycle and thus, the two variables $customerid and $holidayid are not defined.

while($booking = mysql_fetch_array($getbooking)) {
    $customerid = $booking['customerid'];
    $holidayid = $booking['holidayid'];
}

Solution: Check if the result was empty.

if (mysql_num_rows($getbooking) < 1) {
    die('Booking not found.');
}

$getcustomer = mysql_query("SELECT *
    FROM tblcustomers
    WHERE tblcustomers.customerid = $customerid");

Plus: Never use the @ operator! Ignoring the error message does not resolve the underlying problem.

Niko
  • 26,516
  • 9
  • 93
  • 110
1

Although you've done this:

@$customerid = 'blah';

This only suppresses errors for that line. You're trying to access this variable a few lines down. Your best bet is to define the variable at the very top of your script, and then override it if conditionals are met.

$customerid = null;
hohner
  • 11,498
  • 8
  • 49
  • 84
  • 1
    And (especially as OP is just learning) it's not a good idea to simply suppress errors, better to fix them – Mark Baker Jan 12 '13 at 11:24
0

Use isset to determine if a variable is set

Emil Aspman
  • 996
  • 1
  • 17
  • 26