douzi1991 2013-08-20 23:05
浏览 29
已采纳

客户端和服务器php sql bug

I just cant figure out what is wrong with this code.

I am just sending a username and password to the server, then server sending back a response. Server write to database with no problem, but in the client side sometimes it doesn't reach inside the if(xmlhttp.readyState==4 && xmlhttp.status==200). And after it execute the line alert('login5'), the jquery animation reset. I know it is the php problem, but I have no idea why it sometimes works but sometimes doesnt, any help is appreciated.

<script type = "text/javascript">

function sendLogin(){

    var xmlhttp;
    var getString;
    var url = "login.php";

    var username=document.getElementById('name').value;
    var password=document.getElementById('pw').value;
    var url= url+ "?username="+username+"&password="+password;


    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("get", url , true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){

            alert("reached inside");
            getString = xmlhttp.responseText;
            alert(getString);
        }
    }

    xmlhttp.send();
    alert('login5');
    //problem here, have to wait a while
}

</script>  

html code:

        <form id="logInBoxes">
            <input type="text" placeholder="username" id='name' size="15px">
            <input type="password" placeholder="pw" id='pw' size="10px">
            <input type="submit" value="Log In" onclick='sendLogin()'>
        </form>

php code:

<?php
$username= $_GET['username'];
$password= $_GET['password'];

$salt = mcrypt_create_iv(32, MCRYPT_RAND);
$password = crypt($password, $salt);

$salt = mysql_real_escape_string($salt);
$password = mysql_real_escape_string($password);
$sql = mysqli_connect('localhost','root','','housescale');
// Check connection
if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


mysqli_query($sql, "INSERT INTO user (username, password, salt) 
                VALUE 
                ('$username', '$password','$salt')")   or    trigger_error(mysql_error()); 



mysqli_close($sql);

echo $username;


?>

edit: it works if I do alert('login5') 9 more times in a loop. What exactly does this delay fix imply?

  • 写回答

1条回答 默认 最新

  • douxu0550 2013-08-21 03:00
    关注

    Here's the code I used that worked on my end:

    test.html

    <!DOCTYPE html>
    <html>
    <head>
        <script type = "text/javascript">
    
        function sendLogin() {
    
            var xmlhttp;
            var getString;
            var url = "login.php";
    
            var username = document.getElementById( 'name' ).value;
            var password = document.getElementById( 'pw' ).value;
            url = url + "?username=" + username + "&password=" + password; //Don't Need to Re-Declare url Variable
    
            if( window.XMLHttpRequest ) {         // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {                              // code for IE6, IE5
                xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
            }
    
            xmlhttp.open( "get", url , true );
            xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
    
            xmlhttp.onreadystatechange = function() {
    
                if( xmlhttp.readyState == 4 ) {
    
                    if( xmlhttp.status == 200 ) {   //Separated readyState and Status
    
                        alert( "reached inside" );
                        getString = xmlhttp.responseText;
                        alert( getString );
                    }
                }
            };  //Missed Semi-Colon Here
    
            xmlhttp.send();
            alert( 'login5' );
        }
    </script>
    </head>
    <body>
        <form id="logInBoxes">
            <input type="text" placeholder="username" id='name' size="15px">
            <input type="password" placeholder="pw" id='pw' size="10px">
            <input type="submit" value="Log In" onclick='sendLogin()'>
        </form>
    </body>
    </html>
    

    login.php

    <?php
        $username= $_GET['username'];
        $password= $_GET['password'];
    
        $salt = mcrypt_create_iv( 32, MCRYPT_RAND );
        $password = crypt( $password, $salt );
    
        $sql = mysqli_connect( 'localhost', 'root', '', 'housescale' );
        $salt = mysqli_real_escape_string( $sql, $salt );
        $password = mysqli_real_escape_string( $sql, $password );
    
        // Check connection
        if ( mysqli_connect_errno() ){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    
        mysqli_query( $sql, "INSERT INTO user ( username, password, salt ) VALUE ( '$username', '$password', '$salt' )" )
            or trigger_error(mysql_error());
    
        mysqli_close( $sql );
    
        echo $username;
    ?>
    

    I use Mozilla Firefox 23 on Windows 7. My stack is Uniform Server 8.8.2 (PHP 5.4.14 / MySQL 5.5.30).

    I set all the fields in my table to varchar(255) just to be quick. It worked for me with only one issue, sometimes the salt/crypt turned up empty in my database, but that's probably a charset issue because it was able to echo several different combinations just fine.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题