POST不能正确使用mysql

时间:2013-07-03 14:29:15

标签: php jquery html mysql

我在index.php文件中有一个表单,可以发布到第二页,如下所示:

    <form method="post" id="signupform" name="signupform" action="functions.php">
        <label for="user_id">Username:</label><span id="asterix">*</span> <br>
        <input name="user_id" type="text" id="user_id" required><br><br>
        <label for="new_password">Password:</label><span id="asterix">*</span> <br>
        <input name="new_password" type="password" id="new_password" required><br><br>
        <label for="confirm_password">Confirm password:</label><span id="asterix">*</span> <br>
        <input name="confirm_password" id="confirm_password" type="password" required><br><br>
        <label for="new_email">Email:</label><span id="asterix">*</span> <br>
        <input name="new_email" id="new_email" type="email" required>
        <input hidden="hidden" name="action" value="signup">
        <br><br>
        <input id="createaccount" type="submit" value="Create account">
    </form>

提交是通过jQuery submitHandler完成的,如下所示:

                submitHandler: function(signupform) {
                $(signupform).ajaxSubmit({

                    target:'#result',
                    success: function(){
                        $("#result").css("display","block");
                        $('#box').animate({'top':'-800px'},500,function(){
                            $('#overlay').fadeOut('fast');
                            $('#loginmain').hide();
                        });

                    }
                })
            }

结果显示在同一页面的div中,如下所示:

<div id="result" class="success_message" style="display: none"></div>

在第二页functions.php,我有需要在div中显示的内容。

现在的问题是我创建了一个名为db.php的第三个文件来执行数据库操作。

    if (isset($_POST['action']) && $_POST['action'] == "signup") {
    $user_id      = mysql_real_escape_string($_POST["user_id"]);
    $new_password = md5(mysql_real_escape_string($_POST["new_password"]));
    $new_email    = mysql_real_escape_string($_POST["new_email"]);

    $create_account = "INSERT INTO users(username, email, password ) VALUES ('" . $user_id . "','" . $new_password . "','" . $new_email . "')";
    if($create_account){echo "done";}else echo "failed";
    if(mysqli_query($str, $create_account)){
        echo "successful insert";
    }
 }

虽然我在index.php中包含了db.php,但查询没有被执行,因为事实上,表单似乎没有post正确,因为如果我放了echo "test"之后if语句,它没有显示,但是如果我将action="functions.php"替换为action="",那么事情就会奏效,但submithandler行为不正确。

2 个答案:

答案 0 :(得分:2)

您不应在type=hidden声明中指定hidden=hidden而不是input

<input type="hidden" name="action" value="signup">

而不是

<input hidden="hidden" name="action" value="signup">

您是否在if声明之前检查了PHP脚本中的发布值?

答案 1 :(得分:0)

你也应该在functions.php中包含db.php,以便让这些函数在那里工作。