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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c