这是我的问题的第二部分:
我现在正在尝试将代码放在一起。如果您不想看我问题的第一部分,那么我会告诉您,我正在试验并制作一个允许用户发布特定城市活动的网站。首先用户使用下拉菜单选择状态,然后在下一页他们使用下拉菜单选择城市。一旦选择了城市,他们就会被带到 city.php,在这里我们使用数据库中的查询来显示人们为该特定城市发布的事件。无论如何,我想扩展城市并将 city.php 变成指向 events.php、jobs.php 或 forsale.php 的链接的索引。当用户点击其中一个链接时,仍会记住特定的城市,并会进行查询以提取这些信息。我只是在编码时遇到问题:
城市下拉菜单中的代码:
while($result = mysqli_fetch_array($doQuery)){
// $result contains id (cid) and name (cname) for each city
// $result - current row
// here we add HTML code for option "dynamically"
echo "<option value='".$result["cid"]."'>".$result["cname"]."</option>";
session_start();
$_SESSION['cname'] = $city;
来自 city.php 的代码:
session_start();
$_SESSION['cname'] = $city;
// import dbconnect.php
// we use require(not include) to stop the script if such file does not exist
// we use "once" because we do not need to establish dbconnection if it already exists
require_once("dbconnect.php");
// all data which we get from cityByState.php are stored in $_POST superglobal array
// in our case we have only one field "city" so we can get city id from $_POST["city"]
// also we use intval function for security purposes. It converts variable to integer.
$cityId = intval($_REQUEST["city"]);
// query which gets all info about required city
$query = "select * from cities where id=$cityId";
// run the query and handle possible errors
if(!($doQuery = mysqli_query($db, $query))){
echo "Can not get info about the city!"; exit();
}
我只是一个初学者,似乎无法理解如何正确使用会话来让我的网站正常工作。我也不确定我会用什么来确保我可以在 city.php 的子页面(事件、工作、待售)上进行正确的查询。