嘿,现在我有了这个,但是当我注册时,只显示 index.php 页面。没有成功消息。注册表完成后,我重定向到 index.php?msg=1 所以我认为这是正确的吗?
<?php
if (isset($_GET['msg'] == 1)) {
echo "You have successfully registered.";
}
require_once('connect.php');
$errors = array();
if (isset($_POST['submit'])) {
if(empty($_POST['username'])){ array_push($errors, 'You did not submit a username'); }
$old_usn = mysqli_query($connect, "SELECT id FROM users WHERE name = '".htmlentities($_POST['username'], ENT_QUOTES)."' LIMIT 1;") or die(mysqli_error());
if (mysqli_num_rows($old_usn) > 0) { array_push($errors, 'This username is already registered.'); }
if (sizeof($errors) == 0) {
$username = htmlentities($_POST['username'], ENT_QUOTES);
$email = htmlentities($_POST['email'], ENT_QUOTES);
mysqli_query($connect, "INSERT INTO users (name, hashed_pw, email, joined)
VALUES ('{$username}', '{$password1}', '{$email}', NOW());") or die ($connection_error);
header('Location: index.php?msg=1');
}
}
?>
最佳答案
if (isset($_GET['msg'] == 1))
这不是执行此操作的正确方法,因为它将始终被视为已设置。
您需要将它们分成两个条件。
如:
if (isset($_GET['msg']) && $_GET['msg'] == 1)
不过,我强烈建议您使用准备好的语句和 password_hash()
。
你现在拥有的,一点都不安全。
如果您确实打算走那条路(我希望您这样做),那么请仔细阅读这些手册以了解password_hash()
和password_verify( )
:
- http://php.net/manual/en/function.password-hash.php
- http://php.net/manual/en/function.password-verify.php
注意:您似乎缺少为 $password1
设置的变量,因此请确保它具有值,否则您的查询将失败。
此外,mysqli_error()
需要一个数据库连接参数。
mysqli_error($connect)
这一行 or die ($connection_error)
将抛出一个 undefined variable 错误,至少对于您在问题中发布的内容而言。
关于php - 提交成功提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47181701/