0

我正在 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
}
?>

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

正如我在前几行中编码的那样,for submit 类型正在正确显示。只是mysql查询不能正常工作,想弄清楚,请

4

1 回答 1

1

你收到这个错误信息了吗?

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获得在 'group.... 附近使用的正确语法

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

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

如果您有权更改表,请更改不在保留关键字列表中的列名,以防止将来再次出现相同的错误。

于 2013-04-28T16:18:18.727 回答