问题描述:

I am having trouble removing an item from the itinerary/array upon clicking a 'Remove location' link.

The value of the location to be removed is passed into the 'remove_loc.php' file as its unique 'loc_id' and assigned to a new variable. Upon doing this I then try to use 'unset()' to delete it from the session array but it doesn't seem to be working.

The value of the location is definitely being passed through to the 'remove_loc.php' file ok as I have run an echo statement at other end to make sure, and am connecting ok to db etc, so I believe the error is in my remove_loc.php file.

Code for itinerary.php (where locations are displayed).Note 'Remove Location' href link:

<?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>';

?>

Code for 'remove_loc.php' (for removing location from session array):

<?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>

相关阅读:
Top