简体   繁体   中英

PHP session variables between pages

I have a form for users to input information into. There are 4 pages to the form. The first page is customer details that users input, which uses POST to send to the next page and on the second page are sent to the mysql database in the "customer" table.

However, the next two pages are both linked to the "hire" table. The data on page 2 is moved to page 3 using POST and data on page 3 is moved to page 4 using POST.

I decided to use session variables to move all the data taken from page 2 and sent to page 3 to move to page 4, where i planned that all the hire data would then be put into the database, however the data being moved using SESSION doesn't seem to want to move to the next page.

I know for sure that the POST method is working and entering the data into the database is working, but it isnt the correct data (eg time is just 00:00:00), meaning the variables aren't moving across the pages. i've searched around but i'm struggling, and i'm new to php so i have only just found out about session variables!

Any help is welcomed.

The third form code:

<?php
session_start();
$_SESSION['time'] = $time;
$_SESSION['date'] = $date;
$_SESSION['length'] = $length;
$_SESSION['numberofpeople'] = $numberofpeople;
$_SESSION['pickuplocation'] = $pickuplocation;
$_SESSION['destination'] = $destination;
$_SESSION['useofbus'] = $useofbus;
$_SESSION['day'] = $day;
$_SESSION['month'] = $month;
$_SESSION['year'] = $year;
$_SESSION['cost'] = $cost;
$_SESSION['customerid'] = $customerid;
$_SESSION['driverid'] = $driverid;
$_SESSION['endtime'] = $endtime;
session_write_close();
?>

The fourth and final page code:

<?php
session_start();
$time = $_SESSION['time'];
$date = $_SESSION['date'];
$length = $_SESSION['length'];
$numberofpeople = $_SESSION['numberofpeople'];
$pickuplocation = $_SESSION['pickuplocation'];
$destination = $_SESSION['destination'];
$useofbus = $_SESSION['useofbus'];
$day = $_SESSION['day'];
$month = $_SESSION['month'];
$year = $_SESSION['year'];
$cost = $_SESSION['cost'];
$customerid = $_SESSION['customerid'];
$driverid = $_SESSION['driverid'];
$endtime = $_SESSION['endtime'];
session_write_close();
?>

<?php

$payment = $_POST['payment'];
$information = $_POST['information'];

$con = mysql_connect("localhost","busassociation","fishie123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("busassociation", $con);


//INSERT INTO DATABASE


$sql = "INSERT INTO hire (customerid, driverid, time, endtime, date, length,     pickuplocation, destination, useofbus, numberofpeople, cost, day, month, year, payment, information) VALUES ('$customerid', '$driverid', '$time', '$endtime', '$date', '$length', '$pickuplocation', '$destination', '$useofbus', '$numberofpeople', '$cost', '$day', '$month', '$year', '$payment', '$information')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";

mysql_close($con);
?>

What is your host ? Sometimes hosts deactivate session, it's really rare but can happen. I know for sure that you could not use session with 'free' host (free is a french FAI who provides low level host).

Go grab some informations about your host.

If you want to be sure that sessions works really fine with PHP, do two testings pages.

page1.php

<?php session_start(); $_SESSION['test']='Earth is our mother'; ?>

page2.php

<?php session_start();
      if(isset($_SESSION['test'])) echo $_SESSION['test'];
      else echo 'session problem';
?>

我认为您应该在第2页而不是第3页中定义会话变量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM