MOZ
MOZ

Reputation: 758

PHP Session data clears after form submit

I'm storing session data in $relnum variable, as shown below:

if (!isset($_SESSION)) {
    session_start();
    $_SESSION['releasen'] =$_POST['release_no'];
    $relnum=  $_SESSION['releasen']; 
}

And displaying it in form text field, as shown below;

<input name="relnu" id="relnu" type="text" value="<?php if ($rel==''){ echo $relnum;} else echo $rel; ?>" readonly="true"/>

I am submitting above Form data in mysql, as shown below;

if (isset($_POST['submitM']))  {
    $faultd=$_POST['faultdistribution'];
    $faultdes=$_POST['faultdescription'];
    $faultsev=$_POST['faultseverity'];
    $faultt=$_POST['faulttype'];
    $faultn=$_POST['faultcmnt'];
    $rel=$_POST['relnu'];
    $query = mysql_query("INSERT INTO `fault` (`fault-cmnt`,`fault-type`, `release_no`, `fault-discription`, `fault-severity`, `fault-distribution`) VALUES ('$faultn','$faultt', '$rel', '$faultd', '$faultsev', '$faultdes')")
        or die(mysql_error());
    echo "Data Added sucessfully";
}

The data is successfully submitted, but after that $relnum variable displays nothing. I am unable to comprehend what is the reason, as i am storing the data in session.

P.S: I am not using unset.

Please help, what i am missing?

Upvotes: 1

Views: 8250

Answers (5)

trpt4him
trpt4him

Reputation: 1906

The session variable is $_SESSION['releasen']. That is what is made available to you every time you have session_start(); at the top of your code. If you want to use $relnum, you'll have to set $relnum equal to $_SESSION['releasen'] on every page load.

Upvotes: 1

harsh4u
harsh4u

Reputation: 2600

You can try below code:

if (isset($_POST['release_no']) && !empty($_POST['release_no'])) {
    session_start();
    $_SESSION['releasen'] =$_POST['release_no'];
    $relnum=  $_SESSION['releasen']; 
}

Upvotes: 1

Dinesh
Dinesh

Reputation: 447

Remember session_start() only store the variable temporary on the server side, check have you session_start() on every page, for checking just echo your session variable on that page, if it did not echo anything then check the previous where from you are making session variable, it can help you sort out your problem by yourself.

Upvotes: 1

Altaf Hussain
Altaf Hussain

Reputation: 5202

You have no need to post the session data in the form, else your code seems fine. You can do it easy way with posting it in form like below:

if (isset($_POST['submitM']))  {
    session_start();
    $faultd=$_POST['faultdistribution'];
    $faultdes=$_POST['faultdescription'];
    $faultsev=$_POST['faultseverity'];
    $faultt=$_POST['faulttype'];
    $faultn=$_POST['faultcmnt'];
    $rel=$_SESSION['releasen'];
    $query = mysql_query("INSERT INTO `fault` (`fault-cmnt`,`fault-type`, `release_no`, `fault-discription`, `fault-severity`, `fault-distribution`) VALUES ('$faultn','$faultt', '$rel', '$faultd', '$faultsev', '$faultdes')")
        or die(mysql_error());
    echo "Data Added sucessfully";
}

it will work fine, please test it.

Thank you

Upvotes: 2

harsh4u
harsh4u

Reputation: 2600

Use

session_unset();
session_destroy();

Hope this help

Upvotes: 0

Related Questions