gpt4 book ai didi

PhP session 传递并组合变量和字符串

转载 作者:行者123 更新时间:2023-11-30 00:31:51 24 4
gpt4 key购买 nike

我的登录代码:

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

选择数据.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>";
?>

实际上 session 变量没有被解析,我收到一个错误:

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

我还有一个问题,我想选择一个名为“Temperaturexyz”的数据库,其中温度我想存储在字符串中,xyz是我通过 session 获得的变量,我想将两者结合起来,这样我就可以得到我可以在查询中使用的变量

最佳答案

关于您的 session :您尝试在登录代码中设置的 session 变量 $_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)

关于PhP session 传递并组合变量和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22445955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com