簡體   English   中英

將 MYSQL 數據放入會話變量以傳遞到下一頁

[英]Place MYSQL data into session variable to pass to next page

我是 PHP 世界的新手,並且已經研究了好幾天,但似乎被卡住了。

我試圖建立一個小型網站作為客戶帳戶區域,后端有一個 MySQL 數據庫。

我有一個帶有登錄表單的網頁,用戶可以在其中輸入用戶名和密碼。 有一個 php 腳本可以檢查 SQL 數據庫以查看是否匹配,如果匹配則將它們重定向到帳戶頁面。 我在網上找到了一個示例腳本,我用來創建這個腳本,這部分效果很好。

我試圖更進一步,並在網頁上顯示匹配的 SQL 記錄中的數據。 從我所做的研究來看,最好的方法是將會話變量傳遞到帳戶頁面。

這是我的設置

第 1 頁 - index.php 這是一個登錄頁面,帶有一個包含用戶名和密碼條目的表單。 提交時,它運行 checklogin.php

第 2 頁 - checklogin.php

<?php
session_start();

$host="localhost:3306"; // Host name 
$username="**********"; // Mysql username 
$password="**********"; // Mysql password 
$db_name="galactek_myecl"; // Database name 
$tbl_name="clients"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// set variables
$_SESSION['office_name'] = mysql_query("SELECT office FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to   file "login_success.php"
session_register("username");
session_register("password"); 
header("location:myecl.php");
}
else {
header( 'Location: http://www.galactek.com/support/offmaint.html' );
}

?>

第 3 頁 - myecl.php 這是我想要顯示該數據的頁面。 目前我試圖只顯示辦公室名稱,但它一直顯示為 0。如果我將辦公室名稱硬編碼到 checklogin.php 頁面上的變量中,它不會出現問題。

<?php
session_start();
//if(!session_is_registered(myusername)){
// header("location:index.php");
// }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<p>You're HERE!</p>

<?php

echo $_SESSION['office_name']

?>

</body>

</html>

我很確定我忽略了一些東西。 我對 PHP 的接觸不多,無法區分問題所在。 任何幫助將不勝感激。

我認為問題出在這里:

$_SESSION['office_name'] = mysql_query("SELECT office FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");

函數mysql_query不返回字符串,而是一個包含查詢結果的對象。 (請參閱文檔。)但實際上,您已經在上一個查詢的結果中找到了 office,因此無需再執行另一個查詢。 您可以只使用您所做的第一個查詢中的變量$result

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
    // Register $myusername, $mypassword and redirect to   file "login_success.php"
    $row = mysql_fetch_assoc($result); //Get an array with the results.
    $office = $row["office"]; //Get the office.
    session_register($office); //Register the office with the session.
    session_register("username");
    session_register("password"); 
    header("location:myecl.php");
}

一些進一步的說明:

首先,應避免使用mysql_函數。 它們已被棄用並將被刪除。 最重要的是,它們容易出現安全問題。 建議改用 PDO 或 MySQLi。 是一個很好的 MySQLi 初學者指南。

二、 session_register("username"); 不注冊變量$username而是文字字符串"username" 我不確定這就是你想要做的。

第三,文檔說明了有關使用session_register的以下內容:

警告:該函數自 PHP 5.3.0 起已棄用,自 PHP 5.4.0 起已移除。

考慮使用這樣的代碼:

$_SESSION['office'] = $row["office"];

是否可以像 myecl.php 中的 echo 語句需要分號一樣簡單?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM