これは、ここからの私の質問の2番目の部分です。
私は今、コードをまとめようとしています。私の質問の最初の部分を見たくない場合は、私が実験していて、ユーザーが特定の都市のイベントを投稿できるサイトを作成していることを教えてください。最初にユーザーはドロップダウンメニューを使用して州を選択し、次のページでドロップダウンメニューを使用して都市を選択します。都市が選択されると、city.phpに移動し、データベース内のクエリを使用して、特定の都市に投稿されたイベントを表示します。とにかく、私は都市を拡張し、city.phpをevents.php、jobs.php、またはforsale.phpのいずれかへのリンクが配置されるインデックスに変換したいと思います。ユーザーがこれらのリンクの1つをクリックすると、特定の都市が引き続き記憶され、それらの情報を引き出すためのクエリが実行されます。コーディングに問題があります:
都市のドロップダウンメニューからのコード:
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のサブページ(イベント、ジョブ、販売)で適切なクエリを実行できることを保証するために何を使用するかもわかりません。