没有在php中插入数据库的值

时间:2014-08-29 05:51:02

标签: php jquery sql ajax

我用php jquery和sql创建了一个用户注册表单  试图通过ajax请求输入数据库中的细节,代码是  执行完美,但值没有进入数据库,我  通过在sql编辑器查询中运行它也检查了我的查询  工作正常,

你能说出错误在哪里吗?

<!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";
    ?>

2 个答案:

答案 0 :(得分:2)

更改此行

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

到这个

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

答案 1 :(得分:0)

我看到了很多......没有错误处理程序的Ajax,只是成功处理程序。为什么不让php返回错误到ajax并输出到控制台IE console.log(some_error)。它将在未来使调试变得更加容易。

我也看到没有sql异常处理。这会向您显示在尝试插入期间该名称不存在任何列。

接下来只是一些调试技巧,祝你好运。