点击“删除位置”链接时,我无法从行程/数组中删除项目。
要删除的位置的值作为其唯一的“loc_id”传递到“remove_loc.php”文件中,并分配给新变量。执行此操作后,我尝试使用'unset()'将其从会话数组中删除,但它似乎不起作用。
该位置的值肯定会被传递到'remove_loc.php'文件,因为我已经在另一端运行了一个echo语句以确保,并且我正在连接ok等db,所以我相信错误是在我的remove_loc.php文件中。
代码为itinerary.php(显示位置)。注意'删除位置'href链接:
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Display contents of itinerary
if(!empty($_SESSION['itinerary'])){
//Retrieve details of each location in array from database
$query = "SELECT * FROM locations WHERE loc_id IN (";
foreach ($_SESSION['itinerary'] as $loc_id=>$value)
{$query.=$loc_id.',';}
$query = substr($query, 0, -1).')ORDER BY loc_id ASC';
$result = mysqli_query($db, $query);
echo'<form action="itinerary.php" method="POST"><table><tr><th colspan="5">Locations in your itinerary<br></br></th></tr><tr>';
//Display locations in array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$loc_id = $row['loc_id'];
echo"<tr><td>{$row['loc_name']}<br></br></td>
<td>{$row['loc_desc']}</td>
<td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
";
}//while
mysqli_close($db);
}//if
else {echo '<p>Your itinerary is empty.<br></br></p>';}
echo '<p>
<a href=submit.php?submit=$submission>Save itinerary</a><br></br>
<a href="user_details.php">Your details</a><br></br>
<a href="logout.php">Logout</a><br></br>
</p>';
?>
'remove_loc.php'的代码(用于从会话数组中删除位置):
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
?>
<html>
<h1>REMOVE LOCATION</h1>
<br></br>
<?php
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Assign ID of location which has been passed to new variable
if(isset($_GET['value']))
$loc_id = $_GET['value'];
//Remove location from sess array?
if(isset($_SESSION['itinerary'][$loc_id])) unset($_SESSION['itinerary'][$loc_id]);
echo 'Removal successful!';
print_r($_SESSION);
?>
</html>