提交后的成功消息

3

你好,现在我有这个问题:当我注册时,只有 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');

}

}

?>
3个回答

1
问题在这里:

if (isset($_GET['msg'] == 1)) {  // These are two different conditions, you have to separate them by using &

将其翻译为中文:

改成:

if ( isset($_GET['msg']) && $_GET['msg'] == 1 ) {

请重试。

您的代码一点也不安全,请使用预处理语句并存储哈希密码而不是明文密码。


我解决了.. 我真是个蠢货,花了2个小时搜索,你猜怎么着.. 我把URL写错了。很抱歉浪费了你的时间,感谢你的帮助! - DutcherGames

1

if (isset($_GET['msg']) && $_GET['msg'] == 1)这才是正确的方式,因为它会被视为已设置。

你需要将它们分成两个条件。

例如:

if (isset($_GET['msg']) && $_GET['msg'] == 1)

我强烈建议您使用准备语句和password_hash()

现在的做法一点都不安全。

如果您打算采用这种方法(我希望您能这样做),那么请仔细阅读password_hash()password_verify()的手册:

注意:您似乎缺少设置$password1变量的值,因此请确保它有一个值,否则您的查询将失败。
另外,mysqli_error()需要一个数据库连接参数。
  • mysqli_error($connect)
这一行or die ($connection_error)会抛出一个未定义变量错误,至少对于您在问题中发布的内容而言。

2
顺便说一下...有趣的事实:不能在表达式的结果上使用isset()(您可以使用“null !== expression”代替) - Flash Thunder
1
@FlashThunder,感谢您的回复。 - Funk Forty Niner
@Fred-ii-,我该怎么做?你是什么意思? - DutcherGames
@DutcherGames 将以下代码放置在您的 PHP 文件顶部,在开头的 <?php 标签下方 --- ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); 看看是否有任何反应。 - Funk Forty Niner
我解决了.. 我真是个蠢货,花了2个小时搜索,你猜怎么着.. 我把URL写错了。很抱歉浪费了你的时间,感谢你的帮助! - DutcherGames
显示剩余3条评论

-1

isset返回true/false。而你正在将布尔值与1进行比较。


那么我需要使用什么,期望 isset 吗? - DutcherGames
尝试显示这两个值的值: echo isset($_GET['msg'] . ", " . $_GET['msg'] == "1" - brahimfes
但它没有显示任何内容?看起来它找不到这行。 - DutcherGames
也许你的 require_once('connect.php') 破坏了你的代码。在 require_once 之前加上 return/exit。 - brahimfes
我解决了.. 我真是个蠢货,花了2个小时搜索,你猜怎么着.. 我把URL写错了。很抱歉浪费了你的时间,感谢你的帮助! - DutcherGames
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接