PHP MySQLi - 变量未从会话中移交

时间:2015-03-01 13:22:43

标签: php session mysqli

我遇到了将会话变量转移到所有相关成员页面的麻烦。我想要做的是首先匹配数据库的用户名和密码输入,一旦找到准确的成员帐户,请提取该特定成员的所有成员表信息。在提取了所有表数据之后,应将每列分配给变量,然后将其存储在会话中以供网站上的进一步使用。

这适用于$ myusername和$ mypassword,但它似乎不适用于任何其他变量。在另一个网站上,我只是回显分配的$变量,并且该值应该弹出。

<?php

session_start();

error_reporting(E_ALL);
ini_set("display_errors", 1);

// include database access information
include '../MySQL/connect_db.php';
$tbl_name = 'GPA_properties';

 // Connect to server and select databse.
$mysqli=new MySQLi("$dbhost", "$dbuser", "$dbpass", "$dbname");
if($mysqli->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
    }

// Define $myusername and $mypassword as well as escape & stripslashes to protect against MySQL injection
$myusername = stripslashes(mysqli_real_escape_string($mysqli, $_POST['myusername']));
$mypassword = stripslashes(mysqli_real_escape_string($mysqli, $_POST['mypassword']));

// Define query
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' AND password='$mypassword'";
    if(!$result = $mysqli->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }

// count how many rows are returned
if (mysqli_num_rows($result)==1) {
    // Register $myusername, $mypassword
         $_SESSION['myusername']=$myusername;
         $_SESSION['mypassword']=$mypassword;

    // register all other data as blank first
        $activation_date = "";
        $status = "";
        $holidex = "";
        $gpa_email = "";
        $gm = "";
        $gm_email = "";
        $phone_cc = "";
        $phone = "";
        $property = "";
        $address1 = "";
        $address2 = "";
        $zip = "";
        $city = "";
        $country = "";

        // grab all other information based on username
        if ($stmt = $mysqli->prepare("SELECT Activation_Date, status, Holidex, Property_Name, GPA_Distribution_Email, General_Manager, GM_Email, Address_1, Address_2, ZIP, City, State, Country, Phone_CC, Phone FROM $tbl_name WHERE username=?")) {
            $stmt->bind_param("s", $myusername);
            $stmt->execute();
            $stmt->bind_result($activation_date, $status, $holidex, $property, $gpa_email, $gm, $gm_email, $address1, $address2, $zip, $city, $state, $country, $phone_cc, $phone);
            $stmt->fetch();
            }

        // define variables with acquired information
            $_SESSION['Activation_Date']=$activation_date;
            $_SESSION['status']=$status;
            $_SESSION['Holidex']=$holidex;
            $_SESSION['Property_Name']=$property;
            $_SESSION['GPA_Distribution_Email']=$gpa_email;
            $_SESSION['General_Manager']=$gm;
            $_SESSION['GM_Email']=$gm_email;
            $_SESSION['Address_1']=$address1;
            $_SESSION['Address_2']=$address2;
            $_SESSION['ZIP']=$zip;
            $_SESSION['City']=$city;
            $_SESSION['State']=$state;
            $_SESSION['Country']=$country;
            $_SESSION['Phone_CC']=$phone_cc;
            $_SESSION['Phone']=$phone;

            $stmt->close();

    // redirect to login_success.php
        header("location:login_success.php");
        exit;

    } else {
        echo "Wrong Username or Password";
    }

$mysqli->close();

?>

有人介意看看吗? 感谢

1 个答案:

答案 0 :(得分:0)

status是MySQL中的保留关键字。

尝试使用第二个查询:

"SELECT Activation_Date, `status`, Holidex, Property_Name, GPA_Distribution_Email, General_Manager, GM_Email, Address_1, Address_2, ZIP, City, State, Country, Phone_CC, Phone FROM $tbl_name WHERE username=?"

你会注意到单词状态周围的反引号(`)。这告诉MySQL它应该按原样读取,而不是作为关键字。