dousi6701 2017-05-21 23:10
浏览 78

PHP - AJAX - mysql - 无法将数据插入数据库(不使用AJAX时工作正常)

I have this "register users" file in which I have a form, I'll simplify in here what I have:

 <form action="" method="POST">
    <label for="user" class="control-label">User </label>
    <input type="text" name="user" class="form-control" id="user"  value="" required=""/>
    <label for="user" class="control-label">Password1 </label>
    <input type="text" name="password1" class="form-control" id="password1"  value="" required=""/>
    <label for="user" class="control-label">Password2 </label>
    <input type="text" name="password2" class="form-control" id="password2"  value="" required=""/>
    <button type="button" value="signUp" name="submit" class="btn btn-lg btn-primary btn-block" onClick="register()">Sign up!</button>

As you can see, there is an event in there, in a JS file. This file has all the vaidations of the inputs and it works just fine (I don't think it's relevant, so I won't post it). It also has the AJAX call to the PHP file that will insert the data into the database.

 function register(){

 if(validationRegister()){
        $.ajax({

                url: "http://localhost/myProject/extras/processSignUp.php", 
                type: "POST",
                data: {"user": user,
                       "password": password,
                       "password2": password2,
                       },
                dataType: "html",
                cache: false,
                beforeSend: function() {    
                    console.log("Processing...");
                },
                success: 
                      function(data){
                        if(data == "Registered"){
                           window.location.href = "http://localhost/myProject/index.php";
                        }else{
                           window.location.href = "http://localhost/myProject/signUp.php";
                        }
                    }

    });

}else{
    alert("Incorrect data");
}

}

And this is the PHP file:

 <?php

    include_once "connection.php"; --> this has all the data for the connection to the database 

    if($_POST['user'] == '' || $_POST['password'] == '' || $_POST['password2'] == ''){ 
        echo 'Fill all the information'; 
    }else{ 

        $sql = 'SELECT * FROM `user`'; 
        $rec = mysqli_query($con, $sql); 
        $verify_user = 0; 

        while($result = mysqli_fetch_object($rec)){ 
            if($result->user == $_POST['user']){ 
                $verify_user = 1; 
            } 
        } 

        if($verify_user == 0){ 
            if($_POST['password'] == $_POST['password2']){ 
                $user = $_POST['user']; 
                $password = $_POST['password'];  
                $sql = "INSERT INTO user (user,password) VALUES ('$user','$password')";
                mysqli_query($con, $sql); 

                echo "Registered"; 
            }else{ 
                echo "Passwords do not match"; 
            } 
        }else{ 
            echo "This user has already been registered"; 
        }
    } 

?>

The PHP code, works great when used on its own (it used to be at the beginning of the form file, surrounded by if($_POST['submit']){}) But now I want to use it in a separate file, and use AJAX, and I'm unable to register a user :/ the value of data is never "Registered"... Any ideas?

Thanks in advance! :)

  • 写回答

1条回答 默认 最新

  • duansaxf095988 2017-05-21 23:23
    关注

    Please never run this code in a live environment, your code is open to SQL injection and you NEED to hash passwords.

    This line:

    if($_POST['user'] == '' or $_POST['password']){ 
    

    Looks to be your issue. You want to be testing $_POST['password'] somehow, like $_POST['password'] == '' or !isset($_POST['password']).

    Your logic is also horribly constructed, you may want to go look at a few tutorials. e.g. why are you fetching ALL your users just to test if one exists, do that logic in the SQL code itself to avoid fetching an entire table for no reason.

    评论

报告相同问题?

悬赏问题

  • ¥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