1

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?

9
  • Have you used session_start() at the top of your php file where you are using insert query? Commented Sep 12, 2013 at 12:07
  • Remove isset if the session is already started session_start() don't do anything and if isset($_SESSION)then the script will not store the value in the session variable Commented Sep 12, 2013 at 12:08
  • I've removed the isset($_session), but still same problem. Commented Sep 12, 2013 at 12:14
  • @OmerZia Is session_start(); inside ALL your files? Commented Sep 12, 2013 at 12:21
  • Do you have $relnum= $_SESSION['releasen']; on every page that uses that variable? Commented Sep 12, 2013 at 12:22

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Tried, but after submitting form the session variable clears out. (same problem)
1

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.

3 Comments

At first the session variable successfully echo and displays data, but after successfully submitting data, it clears out and displays nothing on the same page.
Make sure you call session_start() before trying to use the session, and always exit() or die() after a redirect header.
i am calling session_start() before using the session.
1

You can try below code:

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

Comments

1

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.

Comments

0

Use

session_unset();
session_destroy();

Hope this help

2 Comments

But i don't want to unset or destroy the session. Issue is why session value clears after submitting form?
Are you check your condition if (!isset($_SESSION)) you can reach in this condition?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.