2017-01-02 109 views
-3

在這段代碼中,我想查詢一個名爲「zipcodes」的表。我想首先檢查輸入的郵政編碼是否是有效的郵政編碼。那麼如果它是一個有效的郵政編碼,我想檢索與匹配的經度,緯度,城市,&狀態的郵政編碼。郵政編碼和檢索它們

然後,在檢索它們之後,我想將它們放到另一個名爲「Users」的表中。一旦進入Users表,我希望能夠將分配給它們的值在我的網站上的各個位置進行回顯。

<?php 

    $query = mysqli_query($con, "SELECT * FROM zipcodes WHERE ZIP='".$user_zip."'"); 

    if(mysqli_num_rows($query) > 0){ 

     echo "Please Reenter A Valid Zip Code"; 
     { 

     }elseif(mysqli_num_rows($query) > 1){ 
     mysqli 
     // Need to put next line of code in users db 
      }elseif (mysqli_num_rows($query) > 1($con, "INSERT INTO ")){ 





     // retrieve from the database the city and state 
    }elseif ($user_zip == true($query = mysqli_query($con, "SELECT * FROM zipcodes ZIP,Latitude,Longitude,City,State WHERE ZIP='".$zip.", Latitude='".$latitude"', Longitude='".$longitude"', City='".$local"', State='".$country"', "'")) { 


    { 


    } 


    ?> 
+1

只需在用戶表中存儲zip記錄的'id'即可。然後在需要位置信息時進行「加入」。也使用參數化查詢。這將開放給SQL注入(可能) – chris85

+3

首先修復無意義的代碼。此代碼必須產生錯誤 – RiggsFolly

+0

@RiggsFolly你的意思是「廢話」,雖然+1 – JBES

回答

0

這是對您的代碼的快速審查。這裏沒有其他答案。我的意見從// :開始。我已經修復了基本的語法並縮進了代碼以使其可讀。

<?php 

$query = mysqli_query($con, "SELECT * FROM zipcodes WHERE ZIP='".$user_zip."'"); 

// : Checking if there are 0 or more rows returned. 
if(mysqli_num_rows($query) > 0) { 
    echo "Please Reenter A Valid Zip Code"; 
// } << : Need a closing bracket here. 

{ // : These curlies must be for decorative purposes? 
} 

// : "If 0 or more rows; else if 1 or more rows". 
// : This'd never run because a previous condition matches; 1 > 0! 
elseif(mysqli_num_rows($query) > 1) { 
    mysqli 
    // Need to put next line of code in users db 
} 

// : You already matched the > 1 condition above. + syntax error! 
elseif (mysqli_num_rows($query) > 1($con, "INSERT INTO ")) { 

// : Fixing that syntax. You probably meant something like: 

elseif (mysqli_num_rows($query) > 1) { 
    mysqli($con, "INSERT INTO [...] "); 
} 

// : This is totally garbled up, both the PHP and the SQL query: 

// retrieve from the database the city and state 
/* 
elseif ($user_zip == true($query = mysqli_query($con, "SELECT * FROM zipcodes ZIP,Latitude,Longitude,City,State WHERE ZIP='".$zip.", Latitude='".$latitude"', Longitude='".$longitude"', City='".$local"', State='".$country"', "'")) { 
*/ 

// : You probably meant something like: 

elseif ($user_zip == true) { 
    $query = mysqli_query($con, 
     "SELECT * FROM zipcodes 
      WHERE ZIP='{$zip}' 
       AND Latitude='{$latitude}' 
       AND Longitude='{$longitude}' 
       AND City='{$local}' 
       AND State='{$country}'"); 
} 

世界上沒有什麼魔法可以使這項工作 - 即使所有的語法都糾正了。我建議你先花一點時間學習PHP(和MySQL)的語法。然後思考你想做什麼的邏輯。然後嘗試在你的代碼中表達。開始編寫最簡單的測試代碼。然後工作你的方式更復雜的東西。祝你好運,快樂學習。

+0

謝謝,我會的。 –

+0

謝謝@Markus AO我將在下個月左右上學,以便我能夠編寫代碼。現在我正在通過教程進行學習,我發現它更容易。但是謝謝你的幫助。這讓我更瞭解PHP –

+0

祝你一切順利。儘量找到優質的教程。不要對5歲以上的東西感到煩惱,比PHP 5.4更老,你錯過了很多自那之後發生的好事。在SO上進行搜索,查看具有更多代表和經驗的用戶鏈接到的來源。這裏的一些教程有一些無望,會教你你後來不得不忘記的黑客和/或粗糙的方法。很高興上面的評論幫助你一些。我會將代碼修改爲適當的形式,但是,我仍然不清楚你希望在你的代碼中做什麼。 –