簡體   English   中英

PhP會話傳遞並結合變量和字符串

[英]PhP session passing and combine variable and string

我的登錄代碼:

<?php
session_start();
$f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
$con=mysql_connect("localhost","root","");
if(! $con)
{
        die('Connection Failed'.mysql_error());
}
mysql_select_db("finaltest",$con);
$result=mysql_query("select * from user");
while($row=mysql_fetch_array($result))
{
    if($row["username"]==$f_usr && $row["password"]==$f_pswd)
        header('Location: selectdata.php');
    else
        echo"Sorry : $f_usr";
}
?>

selectdata.php

<?php
session_start();
$s= $_SESSION['user'];
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("finaltest") or die(mysql_error());
$select="temperature".$s;
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM $select") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>username</th> <th>password</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['username'];
    echo "</td><td>"; 
    echo $row['password'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

實際上會話varibale沒有得到解析,我得到一個錯誤:

Notice: Undefined index: user in C:\xampp\htdocs\bars\selectdata.php on line 3

而我還有另一個問題,我想選擇一個名為“ temperaturexyz”的數據庫,其中要存儲在字符串中的溫度,xyz是我通過會話獲取的變量,我想將兩者結合起來,這樣我就可以獲得一個變量我可以在查詢中使用

關於您的會話:您嘗試在登錄代碼中設置的會話變量$_SESSION['user']並未正確設置該變量。 嘗試:

<?php
 session_start();
 $f_usr= $_POST["userid"];
$f_pswd= $_POST["password"];
$_SESSION['user']=$f_usr;
echo $_SESSION['user'] //<-------- see if this echo's out the value you are expecting

至於您的變量:我假設您的意思是想要一個變量名$ temperaturexyz,但您正在動態創建該變量名。 所以

$select="temperature".$s;
$$select = /* Insert whatever value you need here*/ //<-- you can then call this variable like this (ie. $temperaturexyz)

暫無
暫無

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

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