用PHP和AJAX编写的注册不起作用

loading...


0

基本上,我使用PHP和Ajax进行登录/注册以使用phonegap . 登录有效,如果我尝试使用与现有帐户相同的电子邮件创建帐户,它会响应,但当我尝试创建新帐户时,它表示注册失败.~上面有代码

PHP代码

<?php
$con = mysqli_connect("localhost","root","", "login") or die("connection error");
$email = $_POST['email'];
$password = $_POST['password'];

if(isset($_POST['register']))
{   
    $register = mysqli_num_rows(mysqli_query($con, "SELECT * FROM users WHERE email='$email'"));
    if($register == 0)
    {
        $insert = mysqli_query($con,"INSERT INTO users (email,password) VALUES ('$email','$password')");
        if($insert)                 
            echo "success";
        else
            echo "error";
    }
    else if($register != 0)
        echo "exist";
}
else if(isset($_POST['login']))
{
    $login = mysqli_num_rows(mysqli_query($con, "SELECT * FROM users WHERE email='$email' AND password`='$password'"));
    if($login != 0)
        echo "success";
    else
        echo "error";
}
mysqli_close($con);
?>

AJAX代码//这是登录代码

$("#loginButton").click(function(){
            var email= $.trim($("#email").val());
            var password= $.trim($("#password").val());

            $("#status").text("Authenticating...");
            var loginString ="email="+email+"&password="+password+"&login=";
            $.ajax({
                type: "POST",crossDomain: true, cache: false,
                url: url,
                data: loginString,
                success: function(data){
                    if(data == "success") {
                        $("#status").text("Login Success..!");
                        localStorage.loginstatus = "true";
                        window.location.href = "app.html";
                    }
                    else if(data == "error")
                    {
                        $("#status").text("Login Failed..!");
                    }
                }
            });
        });

   $("#registerButton").click(function(){
            var email= $.trim($("#email").val());
            var password= $.trim($("#password").val());

            $("#status").text("Creating New Account...");
            var dataString="email="+email+"&password="+password+"&register=";
            $.ajax({
                type: "POST",crossDomain: true, cache: false,
                url: url,
                data: dataString,
                success: function(data){
                    if( data == "success")
                        $("#status").text("Registered Success");
                    else if( data == "exist")
                        $("#status").text("Account is already there");
                    else if( data == "error")
                        $("#status").text("Register Failed");
                }
            });

loading...

1回答

  • -1

    我花了很多时间找出问题 . 这是非常简单的错误,请检查SELECT查询的语法错误 .

    1个查询执行,0个成功,1个错误,0个警告查询:SELECT * FROM users WHERE email='ali@akashif.co.uk'AND password` ='123'错误代码:1064您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'='123'附近使用正确的语法

    在破解查询的密码之后还有一个额外的` . 并且查询失败导致mysqli_num_rows始终返回0 .

评论

暂时没有评论!