简体   繁体   English

页面之间的PHP会话变量

[英]PHP session variables between pages

I have a form for users to input information into. 我有一个供用户输入信息的表格。 There are 4 pages to the form. 表格有4页。 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. 第一页是用户输入的客户详细信息,它使用POST发送到下一页,第二页将被发送到“客户”表中的mysql数据库。

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. 使用POST将第2页上的数据移至第3页,使用POST将第3页上的数据移至第4页。

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. 我决定使用会话变量来移动从第2页获取的所有数据,并发送到第3页以移动到第4页,在这里我计划将所有雇用数据放入数据库,但是使用SESSION进行移动的数据却没有似乎不想移至下一页。

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. 我肯定知道POST方法有效并且将数据输入数据库有效,但是它不是正确的数据(例如,时间仅为00:00:00),这意味着变量不会在页面之间移动。 i've searched around but i'm struggling, and i'm new to php so i have only just found out about session variables! 我到处搜索,但是我很挣扎,而且我是php的新手,所以我才刚刚发现会话变量!

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). 我确定您不能与“免费”主机一起使用会话(免费是提供低级主机的法国FAI)。

Go grab some informations about your host. 去获取有关主机的一些信息。

If you want to be sure that sessions works really fine with PHP, do two testings pages. 如果要确保会话真的可以在PHP上正常运行,请执行两个测试页。

page1.php page1.php中

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

page2.php 使page2.php

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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