[英]Using $_GET variable in URL with ?id= from MySQL
I am trying to redirect a user to the previous page (topic.php) when they successfully submit a new post. 当他们成功提交新帖子时,我试图将用户重定向到上一页(topic.php)。 That's easy: 这很容易:
header("Location: viewtopic.php?id=" . $topicid . ");
I may be going about this the wrong way, but on success, i want them to be redirected automatically to the address above and display a success message. 我可能会以错误的方式进行操作,但是成功后,我希望将它们自动重定向到上面的地址并显示成功消息。
Some of my postreply.php 我的一些postreply.php
$result = $database->query($sql);
if ($result) {
header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
}else {
echo "Query failed" . print_r($sql);
}
This is on the viewtopic.php: 这是在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 = "";
}
}
Understandably, php thinks the +status=success
is part of the query to fetch the data from the database, thus results in a MySQL error. 可以理解,php认为+status=success
是从数据库中获取数据的查询的一部分,因此导致MySQL错误。
Is there a way of doing this? 有办法吗?
You should use &
instead of +
when passing parameters from the url to the $_GET
super-global in PHP. 在将参数从url传递到PHP中的$_GET
超全局$_GET
时,应使用&
而不是+
。
So, you need to change '+status=success'
to '&status=success'
因此,您需要将'+status=success'
更改为'&status=success'
Also, as noted in the comment you should always validate user input sent via $_POST
/ $_GET
. 另外,如注释中所述,您应始终验证通过$_POST
/ $_GET
发送的用户输入。
in your case, you can use filter_var()
在您的情况下,可以使用filter_var()
if(isset($_GET['success'])){
$statusmsg = filter_var($_GET['success'], FILTER_VALIDATE_STRING);
if($statusmsg) {
echo $statusmsg;
}else{
// unsafe or invalid string passed
}
}
Change here like this: 像这样更改此处:
header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');
viewtopic.php viewtopic.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 = "";
}
}
header('Location: /community/viewtopic.php?id=' . $topicid . '+status=success');
在此行中的“ +”位置使用“&”。您将能够获取URL中传递的状态值。
Replace +
with &
like this : 用&
替换+
,如下所示:
header('Location: /community/viewtopic.php?id=' . $topicid . '&status=success');
Now the $_GET['status']
catch the status value 现在$_GET['status']
捕获状态值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.