如何使用 HTML 链接将变量从一个 PHP 文件传递到另一个文件?
How to pass variables from one PHP file to another using HTML links?
//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table 输出:
此代码将 select 数据。此外,每一行都有对 order.php
的引用。当用户单击引用(<a href>
子句)时,它会打开 order.php
,我需要知道用户 select 编辑了哪一行来处理这些数据。
将代码更改为:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
在 order.php
中,您可以使用 $_GET["city"]
和 $_GET["country"]
变量访问这些值,这些变量包含 <a href>
link 上的值上一页。例如,运行 echo $_GET["city"];
将输出城市名称。
编辑: 正如@Rizier123 所指出的,如果您的数据库包含同一城市或国家的多个条目,则使用唯一 ID 可能更容易出错。您应该考虑在 table 结构中引入一个 ID,然后在 link 到 order.php
.
中使用它
//DB CONNECTION
$sql = "SELECT `city`,`country` from infotab";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row["city"].$row["country"]"<a href='order.php'>order</a>"; }
Table 输出:
此代码将 select 数据。此外,每一行都有对 order.php
的引用。当用户单击引用(<a href>
子句)时,它会打开 order.php
,我需要知道用户 select 编辑了哪一行来处理这些数据。
将代码更改为:
while ($row = $result->fetch_assoc()) {
echo $row["city"] . $row["country"] . "<a href='order.php?city=" . $row["city"] . "&country=" . $row["country"] . "'>order</a>";
}
在 order.php
中,您可以使用 $_GET["city"]
和 $_GET["country"]
变量访问这些值,这些变量包含 <a href>
link 上的值上一页。例如,运行 echo $_GET["city"];
将输出城市名称。
编辑: 正如@Rizier123 所指出的,如果您的数据库包含同一城市或国家的多个条目,则使用唯一 ID 可能更容易出错。您应该考虑在 table 结构中引入一个 ID,然后在 link 到 order.php
.