2013-01-12 40 views
-2

可能重複:
PHP: 「Notice: Undefined variable」 and 「Notice: Undefined index」
PHP - Undefined variable注意 - 在PHP中未定義的變量

我做PHP的第一次,很享受但它停留在一個錯誤是

注意:未定義變量:customerid在 C:\ XAMPP \ htdocs中\上線cravendale \ showconfirm.php 32

說明:未定義變量:節假日ID在 C:\ XAMPP \ htdocs中\上線cravendale \ showconfirm.php 43

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

我研究這個錯誤很多,我得到的最接近的線索是$客戶ID和$holidayid之前把「@」。錯誤消失,但表單中的信息未加載。

任何幫助將不勝感激。

+0

請修剪您的代碼。這很多,我們只需要看看相關的部分。 –

+0

好吧,讓我爲你編輯它 –

+0

[權威指南PHP的isset和空](http://kunststube.net/isset/) – deceze

回答

0

使用isset,以確定是否一個變量被設置

1

雖然你已經做到了這一點:

@$customerid = 'blah'; 

這隻能抑制誤差的該行。你正在嘗試訪問這個變量幾行。最好的辦法是在腳本的最上面定義變量,然後在滿足條件時重寫它。

$customerid = null; 
+1

而且(特別是因爲OP只是學習),它不是一個好主意,只是壓制錯誤,更好地解決它們 –

2

你的第一個查詢做顯然不返回任何東西,

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

therfore以下「而」循環甚至不執行單週期,因此,這兩個變量$客戶ID和$節假日ID不定義。

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"); 

加:永遠不要使用@操作符!忽略該錯誤消息不能解決潛在的問題。

+0

但我通過填寫表格然後提交它進行預訂。所以它不應該是空的 –