gpt4 book ai didi

php - 无法从多个页面中找到变量

转载 作者:行者123 更新时间:2023-11-30 23:06:58 24 4
gpt4 key购买 nike

我很困惑,是的,一切都会在数据库中。而且,确实有些深奥。有4页PHP关注这个问题。首先,我需要解释一下,你需要知道站点地图。站点地图将像这样 forum.php --> forum-topic --> discussion.php --> deleteTopic .php.

所以,我将解释得更清楚,如下所示。

Discussion.php。此页面用于转到新页面以删除主题。因此,我将像 topic_id 这样的变量传递给 deleteTopic.php,因为我必须从 discussion.php 中检索值。和下面这样的东西。

<a href=\"deleteTopic.php?topic_id=$topic_id\">DELETE</a>

DeleteTopic.php。此页面用于删除所选主题并从 discussion.php 中检索 topic_id

$topic_id = $_REQUEST['topic_id'];    
$sql = "DELETE FROM forum_topic WHERE topic_id = $topic_id ";
mysql_query($sql);
echo "Topic has been deleted!";
header ('Location:forum-topic.php');
exit();
?>

是的,它已成功删除所选主题,但它在 forum-topic.php 上显示了一些错误,例如 未识别的 thread_id 和 thread_name。我意识到我已经在 forum.php 中检索到该值。因为在 deleteTopic.php 中,它有一个来自 forum.php 的值。所以代码应该是这样的。

Forum.php。这是选择的线程转到 forum-topic.php 的地方。因此,我必须传递像 thread_id 和 thread_name 这样的变量。

<a href=\"forum-topic.php?thread_id=$thread_id&thread_name=$thread_name\">$thread_name</a>

那么,你能告诉我应该用这个做什么吗?我不知道我应该在哪里检索变量并将其从 discussion.php 传递到 deleteTopic.php 并在之后导航到 forum-topic.php已成功删除所选主题。不幸的是,forum-topic.php 有一些来自 forum.php 的值。

那么,你能告诉我应该从哪里开始吗?你能带我去吗?非常感谢您的任何答复。干杯!

最佳答案

如果要存储thread_id 和name 的值,将其存储在session 中并在需要时检索它们。

因此,在 forum-topic.php 中,首先检查您是否有新的线程变量,并将它们添加到 session 中,否则,如果您来自 deleteTopic.php,则可以使用 session 中现有的线程变量。总体思路是这样的-

<?php
session_start();
if(isset($_GET['thread_id']) && isset($_GET['thread_name'])){
//If the GET parameters are available. Store them.
$_SESSION['thread_id'] = rawurldecode($_GET['thread_id']);
$_SESSION['thread_name'] = rawurldecode($_GET['thread_name']);
}else{
if(!isset($_SESSION['thread_id']) && !isset($_SESSION['thread_name'])){
//Error.
die('error'); //Or you can redirect.
}
}
/*
The session variables have been initialised.
*/
$t_id = $_SESSION['thread_id'];
$t_name = $_SESSION['thread_name'];
/*
From here on, instead of using $_GET['thread_id'] and $_GET['thread_name'], use $t_id and $t_name.
*/
//Rest of your code.
?>

主要问题在于从表中删除并保留线程变量后返回时参数的处理。

关于php - 无法从多个页面中找到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21470920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com