简体   繁体   English

值未使用php插入数据库

[英]Values not inserting in database with php

I have created a user register form with php jquery and sql and i am trying to enter the details in database via ajax request, code is executing perfectly but values are not entering in the database, and i have checked my query too by running it in the sql editor query also working fine, 我用php jquery和sql创建了一个用户注册表单,我试图通过ajax请求在数据库中输入详细信息,代码执行得很好,但是值没有输入数据库,我也通过在其中运行查询来检查我的查询sql编辑器查询也可以正常工作,

can you tell where is the error ? 你能知道错误在哪里吗?

<!DOCTYPE html>
        <html>
            <head>
                <title>Login Register Test</title>

                <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

    /**
     * Created by pratyush on 8/28/14.
     */
    $(function(){
        $("input[name='btn_submit_reg']").click(function(){
            registerUser();
        });

        $("input[name='btn_submit_login']").click(function(){
            loginUser();
        });
    });



    function registerUser(){

        if(IsValidFormReg()){

            var frm = $(".register").serialize();

            $.ajax({
                url : 'modal/registerdao.php',
                type : 'POST',
                data : frm,
                success : function(result) {

                    if (result.indexOf("correct") > -1) {
                        alert(frm);

                        window.location.replace("registrationconfirm.php");

                    }

                }

            });

        }

    }


    function IsValidFormReg()
    {

        var valid= true;

        var username = $("input[name='username_reg']").val();
        var userpass = $("input[name='userpass_reg']").val();
        var email = $("input[name='useremail_reg']").val();


        if(username.length==0){
            valid = false;

            $("input[name='username_reg']").addClass("formerror");
        }

        if(userpass.length==0){
            valid = false;

            $("input[name='userpass_reg']").addClass("formerror");
        }

        if(email.length==0){
            valid = false;
            $("input[name='useremail_reg']").addClass("formerror");

        }
        else{
            if(checkemail(email)==false){
                valid = false;
                $("input[name='useremail_reg']").addClass("formerror");
                alert("please enter valid email");

            }
        }
        if(!valid)
            $(".formentrieserror").html("&nbsp;&nbsp;&nbsp;&nbsp;Please fill correct form entries...");
        else
            $(".formentrieserror").html("&nbsp;");
        return valid;
    }
    function checkemail(email){

        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if(email.length>0){

            if (!filter.test(email))
                return false;
            else
                return true;
        }
        else
            return false;
    }



    function loginUser(){}


                <style>
                    .formerror{border: solid 2px red;}
                </style>
            </head>
            <body>

            <h2>Login Form</h2> <br><br>
            <form class="login">
                <input type="text" name="username_login" placeholder="user name"> <br> <br>
                <input type="password" name="userpass_login" placeholder="password"><br> <br>

                <input type="button" name="btn_submit_login" value="Login">
            </form>
            <br><br>
            <h2>Registration Form</h2>
            <br><br>

            <form class="register">
                <input type="text" name="username_reg" placeholder="user name"> <br> <br>
                <input type="password" name="userpass_reg" placeholder="password"><br> <br>
                <input type="email" name="useremail_reg" placeholder="email"><br> <br>

                <input type="button" name="btn_submit_reg" value="Register">
            </form>
            <div class="formentrieserror"></div>
            </body>
        </html>

    //registerDao.php..................................//


    <?php



    class RegisterUserInfo{

        public $userName;
        public $userPassword;
        public $userEmail;
    }



    class userRegisterDao {

        function RegisterUser($registration_info) {
            include_once ("database.php");

            $qry = "insert into userdetails(
                    userName,
                    userPassword,
                    userEmail)
                    values('".$registration_info->userName."','"
                        .$registration_info->userPassword."','"
                        .$registration_info->userEmail."')";

            return Database::executeQuery($qry);                    // return true or false

        }
    }



    $userName = mysql_escape_string($_REQUEST ['userName']);
    $userPassword = mysql_escape_string($_REQUEST ['userPassword'] );
    $userEmail = mysql_escape_string($_REQUEST ['userEmail'] );

    $registration_info = new RegisterUserInfo();

    $registration_info->userName=$userName;
    $registration_info->userPassword=$userPassword;
    $registration_info->userEmail=$userEmail;


    $dao = new userRegisterDao();
    $insert = $dao->RegisterUser($registration_info);

    if($insert===true){

        echo "correct";
    }
    else
        echo "invalid";
    ?>

Change this line 更改此行

   $userName = mysql_escape_string($_REQUEST ['userName']);
   $userPassword = mysql_escape_string($_REQUEST ['userPassword'] );
   $userEmail = mysql_escape_string($_REQUEST ['userEmail'] );

to this 对此

    $userName = mysql_escape_string($_REQUEST ['username_reg']);
                                           /*  changed ^^ */
    $userPassword = mysql_escape_string($_REQUEST ['userpass_reg'] );
                                           /*  changed ^^ */
    $userEmail = mysql_escape_string($_REQUEST ['useremail_reg'] );
                                           /*  changed ^^ */

I see this alot... Ajax with no error handler, just success handlers. 我看到了很多...没有错误处理程序的Ajax,只有成功处理程序。 Why not have php return errors to ajax and output to console IE console.log( some_error ) . 为什么不让php向ajax返回错误并输出到控制台IE console.log(some_error)。 It will make debugging a lot easier in the future. 将来,它将使调试变得更加容易。

I also see that there is no sql exception handling. 我也看到没有sql异常处理。 That would have shown you that no column exists by that name during attempted insertions. 那将向您显示在尝试插入期间该名称不存在任何列。

just a few debugging tips going forward, good luck. 只是一些调试技巧,祝您好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM