我正在尝试构建一个简单的数据库搜索页面,这是一个帮助我学习的个人项目。我将构建一个表单来搜索我的 MTG 卡收藏(大得令人痛苦)。我已经建立了数据库,并且可以查询。在我的学习之旅中,我遇到了困难。这堵墙的事实是我无法获取 $GET 变量来重新分配给变量以供进一步使用。为了方便起见,我将代码精简为只有一个变量,即使在这个简单的代码上也能完成任何事情。这伤害了我软弱的学生大脑==
function search_mtg($_get) {
$name = $_GET["name"];
}
echo "Directly -->" . $_GET["name"] . "<br>";
echo "Reassigned -->" . $name;
直接调用可以很好地打印 key 。 。 .重新分配的变量不包含任何内容,我只是不明白为什么。
如果有帮助的话,这是(极其简化的)形式。
<form action= "testResult.php" method="get">
Name: <input type="text" name="name"><br>
input type="submit">
</form>
如果我无法让一个变量通过,我就没有希望继续这个项目 T_T
最佳答案
您有多个概念/逻辑错误:
function search_mtg($_get) {
^^^^^---- do NOT redefine the superglobal variables.
even though variables are case sensitive, don't re-use
a superglobal name, even if _get is not the same as _GET
^^^^^^^^^^---you never call this function
$name = $_GET["name"];
^^^^^---- never gets returned
}
echo "Reassigned -->" . $name;
^^^^^---never gets defined
你至少想要这样的东西:
function search_mtg() {
return $_GET['name'];
}
$name = search_mtg();
echo $name;
关于PHP 无法检索 $_GET 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441152/