douyingyu5573 2017-01-02 16:04
浏览 30
已采纳

邮政编码和检索它们

In this code I want to query a table called "zipcodes". I want to first check to see if the zip code entered is a valid zip code. Then if it is a valid zip code I want to retrieve that zip code with the matching longitude, latitude, city, & state.

Then after retrieving them I want to place them into another table called "Users". Once in Users table I want to be able to pull the values that are assigned to them to echo in various places on my website.

 <?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条回答 默认 最新

  • duanming7961 2017-01-02 18:39
    关注

    This is a quick review of your code. There is no other answer to be made here. My comments begin with // :. I've fixed up the basic syntax and indented the code to make it readable.

    <?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}'");
    }
    

    No magic in the world would make this work -- even with all the syntax corrected. I'd recommend you first spend a bit of time learning PHP (and MySQL) syntax. Then think through the logic of what you want to do. Then try to express that in your code. Start with writing test code for the most simple things. Then work your way up to more complicated stuff. Good luck and happy learning.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算