Php 在URL中使用“$\u GET”变量,从MySQL获取?id=

Php 在URL中使用“$\u GET”变量,从MySQL获取?id=,php,mysql,Php,Mysql,当用户成功提交新帖子时,我试图将其重定向到上一页(topic.php)。这很简单: header("Location: viewtopic.php?id=" . $topicid . "); 我可能是走错了路,但一旦成功,我希望他们被自动重定向到上面的地址,并显示一条成功消息 我的一些postreply.php $result = $database->query($sql); if ($result) { header('Location: /community/viewto

当用户成功提交新帖子时,我试图将其重定向到上一页(topic.php)。这很简单:

header("Location: viewtopic.php?id=" . $topicid . ");
我可能是走错了路,但一旦成功,我希望他们被自动重定向到上面的地址,并显示一条成功消息

我的一些postreply.php

$result = $database->query($sql);

if ($result) {
    header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
}else {
    echo "Query failed" . print_r($sql);
}
这在viewtopic.php上:

$statusmsg = $_GET['status'];

  if (isset($statusmsg)) {
if ($statusmsg === "success") {
  $statusmsg = '<div class="alert alert-success" role="alert">
                  <strong>Success!</strong> Your post was submitted.
                </div>';
}else {
  $statusmsg = "";
}
  }
$statusmsg=$\u GET['status'];
如果(isset($statusmsg)){
如果($statusmsg==“成功”){
$statusmsg='1
成功!您的帖子已提交。
';
}否则{
$statusmsg=“”;
}
}
可以理解,php认为
+status=success
是从数据库获取数据的查询的一部分,因此会导致MySQL错误


有没有办法做到这一点?

在PHP中将参数从url传递到
$\u GET
超级全局时,应该使用
&
而不是
+

因此,您需要将
'+status=success'
更改为
'&status=success'

此外,如注释中所述,您应该始终验证通过
$\u POST
/
$\u GET
发送的用户输入

在您的情况下,您可以使用

在此处更改如下:

header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');
viewtock.php

$statusmsg = $_GET['status'];
if (isset($statusmsg) && !empty($statusmsg)) {
    if ($statusmsg == "success") {
      $statusmsg = '<div class="alert alert-success" role="alert">
                      <strong>Success!</strong> Your post was submitted.
                    </div>';
    }else {
      $statusmsg = "";
    }
}
$statusmsg=$\u GET['status'];
if(设置($statusmsg)&&!空($statusmsg)){
如果($statusmsg==“成功”){
$statusmsg='1
成功!您的帖子已提交。
';
}否则{
$statusmsg=“”;
}
}

在此行的“+”处使用“&”。您将能够获得URL中传递的状态值。

+
替换为
&
,如下所示:

header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');

现在,
$\u GET['status']
捕获状态值

,并在将其传递给query之前开始验证用户输入@AlivetoDie我听从了你的建议,非常感谢。
header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');