2017-01-02 110 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上进行搜索,查看具有更多代表和经验的用户链接到的来源。这里的一些教程有一些无望,会教你你后来不得不忘记的黑客和/或粗糙的方法。很高兴上面的评论帮助你一些。我会将代码修改为适当的形式,但是,我仍然不清楚你希望在你的代码中做什么。 –