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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog