php
  • if-statement
  • 2011-11-27 351 views -4 likes 
    -4

    我有一個if語句用於從地址中檢索緯度和長度並將其插入到sql數據庫中。我有表單工作的基本結構,但希望包括空字段檢查和pregmatch,但如果我嘗試輸入更多,如果語句沒有任何反應。PHP忽略其他語句

    // Your Google Maps API key 
    $key = "key"; 
    // Desired address 
    $address = 'http://maps.google.com/maps/geo?q='.$location.'&output=json&oe=utf8&sensor=false&key=$key'; 
    // Retrieve the URL contents 
    $data = file_get_contents($address); 
    // Parse the returned XML file 
    $jsondata = json_decode($data,true); 
    if (is_array($jsondata)&& $jsondata ['Status']['code']==200){ 
        $lat = $jsondata ['Placemark'][0]['Point']['coordinates'][0]; 
        $lon = $jsondata ['Placemark'][0]['Point']['coordinates'][1]; 
    } 
    elseif (mysql_query("INSERT INTO database (type, category, suggest, title, url, description, phone, fax, facebookpage, twitterpage, busemail, number, streetAddress, city, state, zip, country, name, email, latitude, longitude) 
    VALUES ('$type', '$category', '$title', '$url', '$description', '$phone', '$fax', '$facebookpage', '$twitterpage', '$busemail', '$number', '$streetAddress', '$city', '$state', '$zip', '$country', '$name', '$email', '$lat', '$lon')");  { 
    die(header ("Location: http://www.exampleerrorpage.com") . mysql_error()); 
    } 
    else { mail($email, "Subject", $message, $headers); 
    header("Location: http://www.examplecomplete.com/"); 
    } 
    

    在此先感謝您的幫助。

    編輯只有在PHP的基本知識,我還是一個新手。要溫柔。再次感謝。

    編輯 - 表單的地址部分我並不總是在那裏。所以地理編碼並不總是需要執行。

    +0

    你的問題是根本不清楚。請用代碼解釋你正在嘗試的是什麼,你期望發生什麼,以及發生了什麼。 –

    +1

    不好意思?它如何忽略?如果條件爲TRUE則執行,否則執行ELSE。所以,如果ELSE沒有被執行,那是因爲它運行的是IF語句。 – gustavotkg

    +1

    PHP是*永遠不會「忽略」任何東西。你只是有一個邏輯錯誤,我們無法幫助你,因爲你只向我們顯示非errord版本。 – deceze

    回答

    1

    只需再次慢慢地通過你的邏輯思考。你的程序說:

    如果JSON數據是有效的,設置兩個變量$lat$lon
    否則(如果JSON數據是無效的,但)如果的SQL查詢返回truedie發生錯誤,
    其他(如果以上都不是)發送電子郵件。

    你只是有一個非常搞砸if-then-else邏輯。

    邏輯的角度來看,你可能想沿着線的東西:

    if (/* JSON data not valid */) { 
        die('Error'); 
    } 
    
    $lat = ...; 
    $lon = ...; 
    
    // Prevent SQL injection! 
    $lat = mysql_real_escape_string($lat); 
    $lon = mysql_real_escape_string($lon); 
    
    $query = "INSERT INTO ..."; 
    if (!mysql_query($query)) { 
        die(mysql_error()); 
    } 
    
    if (!mail(...)) { 
        die('Error sending mail'); 
    } 
    
    0

    看似愚蠢的,但讓自己的一些方法,如果查詢只是一直在if(你如果總是返回true。)

    下降如果是,請檢查您的if語句。如果你的陳述總是返回true(類似於1 == 1),我不會看到任何其他方式,否則它不會進入其他方式(如1 == 1)

    如果您希望您的語句始終返回false,只需添加& 1! = 1到你的陳述。它應該總是進入事物的其他部分。

    相關問題