2012-03-18 39 views
3

加載我的網頁的時候,我發現了以下兩個錯誤:PHP - 注意:未定義的變量發生兩次

Notice: Undefined variable: realtor in C:\Program Files\EasyPHP-5.3.9\www\cglst\images\addform.php on line 255 

Notice: Undefined variable: phone in C:\Program Files\EasyPHP-5.3.9\www\cglst\images\addform.php on line 256 

我同時定義這些變量,雖然如此,我不明白爲什麼我會收到這些錯誤。這裏是我的代碼:

function addListing() {//if data was provided, insert it into database and confirm 
    //this will allow everything to be sanitized properly 
    require_once "sanitize.php"; 
    $submitted = false; 

    //Checking if values were passed 
    if (isset($_POST['area']) && 
     isset($_POST['price']) && 
     isset($_POST['address']) && 
     isset($_POST['bedrooms']) && 
     isset($_POST['fullbath']) && 
     isset($_POST['halfbath']) && 
     isset($_POST['sqft'])) 
     //if passed, sanitize and set variables accordingly 
     { 
      $area = sanitizeOne(get_post('area'), 'plain'); 
      $price = sanitizeOne(get_post('price'), 'int'); 
      $address = sanitizeOne(get_post('address'), 'plain'); 
      $bedrooms = sanitizeOne(get_post('bedrooms'), 'int'); 
      $fullbath = sanitizeOne(get_post('fullbath'), 'int'); 
      $halfbath = sanitizeOne(get_post('halfbath'), 'int'); 
      $sqft = sanitizeOne(get_post('sqft'), 'int'); 
      $submitted = true; 
     } 

    //optional fields 
    if (isset($_POST['remarks'])) 
     { 
      $remarks = sanitizeOne(get_post('remarks'), 'plain'); 
     } 
    else 
     {$remarks = ' ';} 

    if (isset($_POST['realtor'])) 
     { 
      $remarks = sanitizeOne(get_post('realtor'), 'plain'); 
     } 
    else 
     {$realtor = "Anne-Marie Pelletier";} 

    if (isset($_POST['phone'])) 
     { 
      $remarks = sanitizeOne(get_post('phone'), 'plain'); 
     } 
    else 
     {$phone = "201.710.5500";} 

    if ($submitted) { 
     $query = 'PREPARE statement FROM "INSERT INTO bix(area, price, address, bedrooms, 
       fullbath, halfbath, sqft, remarks, realtor, phone) VALUES(?,?,?,?,?,?,?,?,?,?)"'; 
     mysql_query($query); 
     $query = 'SET 
        @area = "' . $area . '"' . 
        '@price = "' . $price . '"' . 
        '@address = "' . $address . '"' . 
        '@bedrooms = "' . $bedrooms . '"' . 
        '@fullbath = "' . $fullbath . '"' . 
        '@halfbath = "' . $halfbath . '"' . 
        '@sqft = "' . $sqft . '"' . 
        '@remarks = "' . $remarks . '"' . 
        '@realtor = "' . $realtor . '"' . //line 255 
        '@phone = "' . $phone . '"'; //line 256 
     mysql_query($query); 
     $query = 'EXECUTE statement USING @area,@price,@address,@bedrooms,@fullbath,@halfbath,@sqft,@remarks,@realtor,@phone'; 
     mysql_query($query); 
     $query = 'DEALLOCATE PREPARE statement'; 
     mysql_query($query); 
     return true; 
    } 
} 
function get_post($var) 
{ 
    return mysql_real_escape_string($_POST[$var]); 
} 

如果提交這簡直是添加到一個數據庫中的條目(頁面提交一個表單到itsself做到這一點)

回答

2

你的問題就在這裏,一個cut'n '粘貼錯誤;

if (isset($_POST['realtor'])) 
{ 
    $remarks = sanitizeOne(get_post('realtor'), 'plain'); 
} 
else 
    {$realtor = "Anne-Marie Pelletier";} 

如果經紀人設置爲之後的參數,分配後變量的值,以$remarks而不是向$realtor

$phone具有完全相同的問題。

+0

啊,哎呀!謝謝。 – mavix 2012-03-18 22:29:30

+0

好的新問題:它將條目添加到我的數據庫中,但每個字段都是空的...... – mavix 2012-03-18 22:31:27

+0

您的'SET'查詢沒有逗號分隔參數,我敢肯定引號需要''',而不是'''爲字符串,試着回顯一下,看看它是否像PhpMyAdmin一樣運行。無論哪種方式,你都不檢查任何返回值,這將使它更容易診斷。 – 2012-03-18 22:35:42

0

您從不向$ realtor或$ phone分配任何東西。

if (isset($_POST['realtor'])) 
     { 
      $remarks = sanitizeOne(get_post('realtor'), 'plain'); 
     } 

您可能意味着使用$realtor = sanitizeOne(get_post('realtor'), 'plain');

同樣爲$電話。

1

如果手機值傳遞要設置變量到手機內容的言論,如果沒有設置要設置固定電話

變化:

if (isset($_POST['phone'])) 
    { 
     $remarks = sanitizeOne(get_post('phone'), 'plain'); 
    } 
else 
    {$phone = "201.710.5500";} 

if (isset($_POST['phone'])) 
    { 
     $phone = sanitizeOne(get_post('phone'), 'plain'); 
    } 
else 
    {$phone = "201.710.5500";} 

同樣適用於房地產經紀人

要調試全空問題,請嘗試記錄沒有房地產經紀人或電話的記錄,即使用代碼中的默認值。如果你存儲了這兩個值,那麼問題出現在santizeOne中,將代碼發佈給我們來幫助。如果它不嘗試捕獲所有首先查詢的輸出並將其發佈。