dragon201401 2015-10-15 07:57
浏览 12
已采纳

Mysql没有获取所有数据[关闭]

<?php
    include('config.php');

    $isd=mysql_real_escape_string( $_GET['q'] );
    $sql=mysql_query("SELECT zip FROM zipcod where abb='$isd' ") or ;


    while( $row=mysql_fetch_array( $sql, MYSQL_ASSOC ) ){
        $zip=$row['zip'];
        echo '<option>'.$zip.'</option> ';
    }
?>

This is the ajax

<script>
    function showUser(str) {
        if (str=="") {
            document.getElementById("zzips").innerHTML="";
            return;
        }
        if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                document.getElementById("zzips").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getstate.php?q="+str,true);
        xmlhttp.send();
    }
</script>

HTML

<select name="states"onchange="showUser(this.value)">
    <option value="AL">AL</option>
    <option value="AK">AK</option>
    <option value="AZ">AZ</option>
    <option>AR</option>
    <option>CA</option>
    <option>CO</option>
    <option>CT</option>
    <option>DE</option>
    <option>FL</option>
    <option>GA</option>
    <option>HI</option>
    <option>ID</option>
    <option>IL</option>
    <option>IN</option>
    <option>IA</option>
    <option>KS</option>
    <option>KY</option>
    <option>LA</option>
    <option>ME</option>
    <option>MD</option>
    <option>MA</option>
    <option>MI</option>
    <option>MN</option>
    <option>MS</option>
    <option>MS</option>
    <option>MT</option>
    <option>NE</option>
    <option>NV</option>
    <option>NH</option>
    <option>NJ</option>
    <option>NM</option>
    <option>NY</option>
    <option>NC</option>
    <option>ND</option>
    <option>OH</option>
    <option>OK</option>
    <option>OR</option>
    <option>PA</option>
    <option>RI</option>
    <option>SC</option>
    <option>SD</option>
    <option>TN</option>
    <option>TX</option>
    <option>UT</option>
    <option>VT</option>
    <option>VA</option>
    <option>WA</option>
    <option>WV</option>
    <option>WI</option>
    <option>WY</option>
</select>

 

This code is fetching Zip codes of some states like CO, NJ, not all. Can someone let me know the error so that it display zip codes according to the states selected in the first drop down

thanks for any help.

</div>
  • 写回答

2条回答 默认 最新

  • duan1989643 2015-10-15 08:06
    关注

    There is a minor error in the php - you omit the condition after the or statement.

    <?php
        include('config.php');
    
        $isd=trim( mysql_real_escape_string( $_GET['q'] ) ); /* or? */
        /* Is the table actually called `zipcod` or `zipcode`? */
        $sql=mysql_query( "SELECT `zip` FROM `zipcod` where trim( `abb` )='$isd';" ) or die('bad mojo');
    
    
        while( $row=mysql_fetch_array( $sql, MYSQL_ASSOC ) ){
            $zip=$row['zip'];
            echo '<option>'.$zip.'</option> ';
        }
        mysql_free_result( $sql );
    
    ?>
    

    To aid debugging javascript / ajax request:

    function showUser(str) {
        var dbg=true;
        if( dbg )console.group('showUser');
    
        if( str=="" ) {
            document.getElementById("zzips").innerHTML="";
            if( dbg )console.warn('Empty string');
            return;
        }
        if( dbg )console.info( 'Initial value passed to ajax function: %s ',str );
    
    
        xmlhttp=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        if( dbg )console.info( 'XHR Object created: %s', xmlhttp );
    
        xmlhttp.onreadystatechange=function() {
            if( xmlhttp.readyState==4 && xmlhttp.status==200 ) {
                document.getElementById("zzips").innerHTML=xmlhttp.responseText;
                if( dbg )console.info('Response received: %s', xmlhttp.responseText )
            }
        }
        xmlhttp.open( "GET", "getstate.php?q="+str, true );
        if( dbg )console.info('Connection opened');
    
        xmlhttp.send();
        if( dbg )console.info('Request sent');
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看