我无法使用php中的Sessions将值从一个页面传递到另一个页面

时间:2015-09-24 04:08:23

标签: php mysql session

我是编程新手。我想从数据库中获取已记录的用户ID并存储在SESSION中,并将该会话ID插入另一个表中。我想我度过了我的一天但不幸的是有一个错误让我疯狂。任何帮助将不胜感激!

数据库btrs的表booking具有库名

booking_id,customer_id,route_id

dbConfig.php

<?php
$dbhost = "localhost";      
$dbname = "btrs"; 
$dbuser = "root";           
$dbpass = "";               
$conn =mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>

Logincheck.php

<?php
session_start();
include('dbConfig.php');
$error  =   ''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['email']) || empty($_POST['password'])) {
        $error = "email or Password is invalid";
    }
    else {
        $email      =   $_POST['email'];
        $password   =   $_POST['password'];
        $email      =   stripslashes($email);
        $password   =   stripslashes($password);
        $email      =   mysql_real_escape_string($email);
        $password   =   mysql_real_escape_string($password);

        $query      =   mysql_query("select * from member where password='$password' AND email='$email'");
        $count      =   mysql_num_rows($query);
        if ($count > 0 ) {
            $row    =   mysql_fetch_array(mysql_query("select * from member where password='$password' AND email='$email'"));
             $id    =   $row['id'];
            // echo $id;
            $_SESSION['id']=$id;
            header("location: profile.php"); 
        } else {
            $error = "Username or Password is invalid";
        }
    }
}
?>

booking.php

<?php
include"dbConfig.php";
include "logincheck.php";
if (isset($_POST['submit'])) {
    $from1          =   $_POST['from'];
    $destination1   =   $_POST['destination'];
    $query          =   mysql_query("SELECT route_id FROM route WHERE pick_from='$from1' AND destination='$destination1'"); 
    $row            =   mysql_fetch_array($query); 
    $route_id       =   $row['route_id'];
    // echo $route_id;

    if ($row!=NULL) {
        $query1 =   "INSERT INTO booking(customer_id,route_id) VALUES ('".$_SESSION['id']."','$route_id')";
        if ($query1==1) {
            echo"very goodddd";
        }
    }
    else {
        echo "good";
    }
}
?>

3 个答案:

答案 0 :(得分:0)

您需要在profile.php文件中启动会话。

在包含dbconfig文件之前,在session_start();profile.php的行顶部添加booking.php

答案 1 :(得分:0)

对于您想要创建会话变量和/或使用会话变量的每个页面,您应该像这样开始代码的最顶层......

<?php
if (!isset($_SESSION)) {
  session_start();
}

快乐的编码!

答案 2 :(得分:0)

在PHP中,你必须先启动会话。

所以使用此代码启动会话。

  

在session_start();

访问此网址以获取更多参考。

http://php.net/manual/en/function.session-start.php