-1

帮助,可能是一个完全的菜鸟问题,但是 home.php 不会显示我是否已经登录。Home.php 已连接到数据库,但我输入了正确的用户名和密码详细信息,它只显示“请先登录”。'

<?php
session_start();
include("config.php");
$sql="SELECT * FROM table_name WHERE username= '{$_SESSION['user']}'" ;
$result=mysql_query($sql,$ms);
if( !mysql_num_rows($result) ) die( 'Please log in first<a href="index.php">Login</a>'          );


echo "You are Welcome ". $_SESSION['user'];


?>

<a href="logout.php">Logout</a> 
4

3 回答 3

1

Check your query again for typos. Mark table names and field names with `.

Note that the mysql_* methods are deprecated. Use PDO instead.

Also check if your $_SESSION variable is filled.

于 2013-07-29T09:52:53.307 回答
0

您的 SQL 查询对我来说似乎是错误的。

对于一个简单的解决方案,请尝试

"SELECT * FROM table_name WHERE username='" . $_SESSION['user'] . "'";

顺便说一句,您最好编写 OO 代码,采用最佳实践(SoC、MVC、模式等)、清理数据并使用准备好的语句而不是此类查询构建。


$_SESSION['user']设置在所有?

请注意,$_SESSION['user']也可能未初始化

于 2013-07-29T09:53:56.903 回答
-1
$sql="SELECT * FROM table_name WHERE username= '{$_SESSION['user']}'" ;

This line of code has error. {$_SESSION['user']} store this value in one variable.

See this example:

$xyz= $_SESSION['user'];
$sql="SELECT * FROM table_name WHERE username= '$xyz'" ;

May be this will work for you.

于 2013-07-29T09:52:41.950 回答