gpt4 book ai didi

php - 数据未插入数据库 PHP MySQL

转载 作者:行者123 更新时间:2023-11-29 13:53:35 25 4
gpt4 key购买 nike

我正在 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 提交类型正确显示。这只是 mysql 查询无法正常工作,想弄清楚,请

最佳答案

您收到此错误消息了吗?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group....

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

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

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

关于php - 数据未插入数据库 PHP MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16264909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com