数据未插入数据库PHP MySQL

时间:2013-04-28 16:17:08

标签: php mysql forms

我在XAMPP中练习一些基本错误处理的注册表单,而我最后点击按钮将数据插入表中,但它没有。我检查了所有内容,表格和行是正确的,数据库是连接的,因为其他页面的数据正在插入但是从这里没有。代码如下,

<?php
/* Register Form */
include "connect.php";
include "links.php";

//check which form is being submitted
$get_submit_type = "";
if (isset($_POST['submit'])){
    echo $get_submit_type = "user_details_submit";
}
else if (isset($_POST['confirm_submit'])){
    echo $get_submit_type ="confirm_submit";
}
else {
    echo "";
}

// acction according to which form is submitted
switch($get_submit_type){
    case "user_details_submit":
?>
        <div style="clear: both; margin: auto; width: 50%; font: normal 12px Verdana;" id="register_details">
            <?php
                //if (isset($_POST['submit'])){

                    //error handling

                    // any field epmty
                    if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['password_confirm']) || empty($_POST['email'])){
                        die($lang['REG_FIELD_EMPTY']);
                    }

                    // password not matching
                    if($_POST['password'] != $_POST['password_confirm']){
                        die($lang['REG_PASS_NOT_MATCHING']);
                    }

                    echo "<hr />";
                    $username = $_POST['username'];
                    $password = md5($_POST['password']);
                    $email = $_POST['email'];
                    echo $lang['REG_USER'] . " = " . $_POST['username'] . "<br />";
                    echo $lang['REG_PASS'] . " = " . md5($_POST['password']) . "<br />";
                    echo $lang['REG_EMAIL'] . " = " . $_POST['email'] . "<br />";
                    echo $lang['REG_NOTICE'] . " = " . $lang['REG_NOTICE_DETAILS'] . "<br />";

                //}
            ?>
    <form name="confirm" method="post" action="#confirmation_details">
        <input type="hidden" name="username" value="<?php echo $username; ?>" />
        <input type="hidden" name="password" value="<?php echo $password; ?>" />
        <input type="hidden" name="email" value="<?php echo $email; ?>" />
        <input type="hidden" name="group" value="standard_user" />
                <button type="submit" name="confirm_submit" class="form-sell"><?php echo $lang['REG_REGISTER_CONFIRM']; ?></button>
            </form>
        </div>
<?php
        break;
    case "confirm_submit":
?>

        <div id="confirmation_details" style="clear: both; width: 75%; margin: auto;">
            <?php
                //if(isset($confirm_submit)){
                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $email = $_POST['email'];
                    $group = $_POST['group'];
                    $query = mysql_query("INSERT INTO users (username,password,email,group) VALUES ('$username','$password','$email','$group')");

                    if(!$query){
                        die("The data was not inserted into the database");
                    }

                    echo $lang['REG_USER_REGISTERED'];

                //}
            ?>
        </div>
<?php
    break;
    default:
?>
<form name="register" method="post" action="#register_details">
<input type="text" class="form-sell" name="username" placeholder="<?php echo $lang['REG_USER']; ?>" id="username" /><br />
<input type="password" class="form-sell" name="password" placeholder="<?php echo $lang['REG_PASS']; ?>" /><br />
<input type="password" class="form-sell" name="password_confirm" placeholder="<?php echo $lang['REG_PASS_CONFIRM']; ?>" /><br />
<input type="text" class="form-sell" name="email" placeholder="<?php echo $lang['REG_EMAIL']; ?>" /><br />
<button type="submit" name="submit" class="form-sell"><?php echo $lang['REG_REGISTER']; ?></button>
</form>
<?php
}
?>

由于我在练习并且此代码不在线,所以我没有使用带有表单数据的真正转义字符串。

正在正确显示提交类型,因为我已在前几行编码。 这只是mysql查询无法正常工作,想弄明白,请

1 个答案:

答案 0 :(得分:1)

您收到此错误消息吗?

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'group ....

附近使用正确的语法

GROUP是保留关键字,恰好是您列的名称。为了避免语法错误,应使用反引号转义列名称。前,

INSERT INTO users (username,password,email,`group`)

如果您有权更改表格,请更改保留关键字列表上不存在的列名称,以防止同一错误再次返回。