deluxenathan deluxenathan - 6 months ago 20
SQL Question

PHP Function - Cannot unset $_SESSION variable

I cannot unset a session variable for a PHP Function,

From the code below, If I enter a review into a site, with the

$_SESSION['KEYWORD'] data it will post the correct data, but if I want to

leaver another review through a different $_SESSION variable,

once it is added into MySQL it displays the value of the first review I entered $_SESSION['KEYWORD'] in this example...

<?php session_start(); ?>
<?php include('db_connect.inc');?>
<link rel="stylesheet" type="text/css" href="style.css" />



<?php
if (isset($_SESSION['SUBURB'])) {
$VAR = $_SESSION['SUBURB'];
Set_Review($VAR);
unset($VAR);

} elseif (isset($_SESSION['KEYWORD'])) {
$VAR = $_SESSION['KEYWORD'];
Set_Review($VAR);
unset($VAR);

} elseif (isset($_SESSION['LOCATION'])) {
$VAR = $_SESSION['LOCATION'];
Set_Review($VAR);
unset($VAR);

} elseif (isset($_SESSION['RATING'])) {
$VAR = $_SESSION['RATING'];
Set_Review($VAR);
unset($VAR);

}
?>


<?php
function Set_Review($VAR) {
include('db_connect.inc');
$result = $conn->prepare("INSERT INTO dog_parks.reviews (review_text, username, date, rating, item) VALUES (:review, :id, :date, :rating, :park_value)");
$result->bindParam(':review', $value1);
$result->bindParam(':id', $value2);
$result->bindParam(':date', $value3);
$result->bindParam(':rating', $value4);
$result->bindParam(':park_value', $value5);

$value1 = $_POST['review'];
$value2 = $_SESSION['id'];
$value3 = $date = date("Y-m-d");
$value4 = $_POST['rating'];
$value5 = trim($VAR, "'\"");
$result->execute();
echo "<h3>Review entry Successful, you will now be redirected to the home page</h3>";
header( "refresh:5; url=index.php" );
unset($VAR);
}
?>

Answer

You are unsetting the $VAR variable, not your $_SESSION variable. Try:

unset($_SESSION['KEYWORD'];