-2

可能重复:
PHP:“注意:未定义的变量”和“注意:未定义的索引”</a>
PHP - 未定义的变量

我是第一次做 PHP,非常享受它,但是遇到了一个错误,那就是

注意:未定义变量:第 32 行 C:\xampp\htdocs\cravendale\showconfirm.php 中的 customerid

注意:未定义变量:第 43 行 C:\xampp\htdocs\cravendale\showconfirm.php 中的 holidayid

代码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 ;        
        }
?>

我已经对此错误进行了很多研究,我得到的最接近的线索是将“@”放在 $customerid 和$holidayid. 错误消失,但未加载表单中的信息。

任何帮助将不胜感激。

4

3 回答 3

2

您的第一个查询显然没有返回任何内容,

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

因此,下面的“while”循环甚至不执行单个循环,因此没有定义两个变量 $customerid 和 $holidayid。

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

解决方法:检查结果是否为空。

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

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

另外:永远不要使用@运算符!忽略错误消息并不能解决根本问题。

于 2013-01-12T11:23:01.500 回答
1

虽然你已经这样做了:

@$customerid = 'blah';

这只会抑制该行的错误。您正试图在几行之后访问此变量。最好的办法是在脚本的最顶部定义变量,然后在满足条件时覆盖它。

$customerid = null;
于 2013-01-12T11:20:32.763 回答
0

用于isset确定是否设置了变量

于 2013-01-12T11:17:56.283 回答