PHP注册表未保存
我有一个HTML注册表单,允许新用户注册到该站点:PHP注册表未保存,php,mysql,Php,Mysql,我有一个HTML注册表单,允许新用户注册到该站点: <form action="register.php" method="POST" class="register-form"> <h1>Create Account</h1> <label> <span>First Name :</span> <input id="firstname" type="text" name
<form action="register.php" method="POST" class="register-form">
<h1>Create Account</h1>
<label>
<span>First Name :</span>
<input id="firstname" type="text" name="firstname" placeholder="Your First Name" autocomplete="off" required/>
</label>
<label>
<span>Surname :</span>
<input id="surname" type="text" name="surname" placeholder="Your Surname" autocomplete="off" required/>
</label>
<label>
<span>Username :</span>
<input id="username" type="text" name="username" placeholder="Your Chosen Username" autocomplete="off" required/>
</label>
<label>
<span>Email :</span>
<input id="email" type="email" name="email" placeholder="Your Email Address" autocomplete="off" required/>
</label>
<label>
<span>Password :</span>
<input id="password" type="password" name="password" placeholder="Your Chosen Password" autocomplete="off" required/>
</label>
<hr>
<input name="action" type="hidden" value="signup" />
<input type="submit" class="btn register btn-success btn-lg" name="submit" value="Register">
</form>
创建帐户
名字:
姓:
用户名:
电邮:
密码:
进入register.php:
<?php
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('gmaps1');
if (!$select_db) {
die("Database Selection Failed" . mysql_error());
}
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])) {
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `users` (firstname, surname, username, password, email) VALUES ('$firstname', '$surname', '$username', '$password', '$email')";
$result = mysql_query($query);
if ($result) {
header("Location: thank_you.html");
} else {
echo 'User Not Created';
}
}
?>
错误检查解决了我的问题-“字段“active”没有默认值”
“用户”表中有一个非活动字段。我把它处理掉了,效果很好。它一定是错误地添加的。
- 您没有获得“数据库连接失败”,因此您成功地与数据库连接
- 最好使用MySQLi或PDO(我的选择)
- 至少使用一个
trim()
,但这只是安全性的一个起点
- 检查所有数据库字段的命名方式是否与Insert语句中处理它们的方式完全相同
- 执行
echo$query
,打开phpMyAdmin(例如),并将输出复制粘贴到SQL字段中,然后发送->您可能会得到一个可以分析的MySQL错误
- 最好将密码散列存储。尝试插入
MD5($password)
(有更好的选择!),登录时进行比较:
如果(MD5($inputPassword)==$passwordhashFromDatabase){}
在打开PHP标记后立即将错误报告添加到文件顶部,例如检查日志文件,如果有任何错误,请共享错误。顺便说一句,您确实计划存储哈希密码,对吗?我希望这不是用于实时站点的。我猜至少有一个字段包含“
字符。由于您既没有转义用户提供的字符串,也没有将其带外传递给MySQL(作为准备语句的参数),因此引号字符将导致错误行为,可被滥用来执行SQL注入攻击!请参阅以了解更多信息。目前,密码哈希密码不像大学项目那样是优先考虑的,它只会在我的本地服务器上运行。哦,是的,这是错误检查,我修改了我的答案以反映这一点。非常感谢。