简体   繁体   中英

PhP session passing and combine variable and string

My login code:

<?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>";
?>

actually the session varibale is not getting parsed i am getting an error:

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

and i have an other problem i want to select a database named "temperaturexyz" where temperature i want to store in a string and xyz is variable that i am getting via the session i want to combine the both so that i can get a variable which i can use in the query

Regarding your session: It appears your session variable $_SESSION['user'] which you attempt to set in your login code is not correctly setting the variable as expected. try:

<?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

As for your variable: I assume you mean you want a variable name $temperaturexyz but you are creating the variable name dynamically. so

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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