PHP Session Array Values Lost On Third Page

383

I have a long data information sheet which I have divided into three pages.
What I have been trying to do is to:

  1. start a session on page 1 to carry over array $sda to page 2,
  2. have a session on page 2 to carry over arrays $sda and $sdb data to page 3,
  3. and on page 3, insert all array data to a database and end the session.

However, on page 3, array $sda from page 1 becomes null and only array $sdb is transferred. What am I failing to do?

Here is my code:
PAGE 1, user fills up the form, values are put into array $sda, user clicks submit

session_start();
 $_SESSION['sda'] = $sda;
 header("location:enrol.php?a=2");

PAGE 2

session_start();
 $sda = $_SESSION['sda'];
 print_r($_SESSION);

user fills up form, values are put into array $sdb, (values in array $sda SHOULD BE retained and passed on to page 3. it doesn't really need to be printed in this page but I did so to check whether I could access its contents from the previous page--which works just fine and prints

Array ( [sda] => Array ( [0] => First [1] => Mid [2] => Last [3] => Nick [4] => Cit [5] => Place [6] => January [7] => 1 [8] => 2012 [10] => Grade 1 [9] => School [11] => Home [12] => [13] => [email protected] [14] => [15] => [16] => Guard [17] => Rel [18] => 12345678 [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [37] => [39] => [41] => [36] => [38] => [40] => ) [sdb] => Array ( [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => a [22] => b [23] => c [24] => d [25] => e [26] => f ) )

user clicks submit (I just want $sda values on the next page. Am I doing it right? :z)

session_start();
 $_SESSION['sda'] = $sda;
 $_SESSION['sdb'] = $sdb;
 header("location:enrol.php?a=3");

PAGE 3

session_start();
 $sda = $_SESSION['sda'];
 $sdb = $_SESSION['sdb'];
 print_r($_SESSION);

ERROR HERE! $SDA VALUES ARE LOST though $sdb values are printed;

Array ( [sda] => [sdb] => Array ( [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => a [22] => b [23] => c [24] => d [25] => e [26] => f ) )

on submit
save to database and destroy session

274

Answer

Solution:

Remove this line from your code which is return after

 user clicks submit (I just want $sda values on the next page. Am I doing it right? :z)

i.e.

  $_SESSION['sda'] = $sda;

People are also looking for solutions to the problem: php - Display results from past days

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.