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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题