我正在创建一个社交网络。我创建了一个个人资料页面和一个新闻源类型的页面,可将您导航到个人资料页面。
我正在尝试根据您在另一个脚本中输入的数据轻松创建独特的基本配置文件布局。我有一个会话变量($_SESSION['id']),我试图抓取它,将用户移动到基于它的页面,并加载与 MySQL 表中的 id 匹配的结果。但是,依赖于 id 的数据似乎不起作用。以下是关键脚本的摘录:
导航菜单到配置文件
<?
function fetch_new_users()
{
$result = mysql_query("SELECT `id`, `username` FROM `users` WHERE username='{$_SESSION['user_login']}'");
$mysql = mysql_fetch_assoc($result);
return $mysql;
}
$users = fetch_new_users();
?>
<?
if (isset($_SESSION["user_login"])) {
echo '
<div class="fnav">
<?php foreach($users as $user): ?>
<p>
<a href="profile.php?id=<?php echo $user[\'id\']; ?>" >Profile</a>
</p>
<?php endforeach; ?>
</div><!-- end fnav -->
登录时创建会话变量
if(isset($_POST["user_login"]) && isset($_POST["password_login"]))
{
$user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]); // filter everything but numbers and letters
$password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]); // filter everything but numbers and letters
$md5password_login = md5($password_login);
$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$md5password_login' LIMIT 1"); // query the person
//Check for their existance
$userCount = mysql_num_rows($sql); //Count the number of rows returned
if($userCount == 1)
{
while($row = mysql_fetch_array($sql))
{
$id = $row["id"];
}
$_SESSION["id"] = $id;
$_SESSION["user_login"] = $user_login;
$_SESSION["password_login"] = $password_login;
exit("<meta http-equiv=\"refresh\" content=\"0\">");
}
else
{
echo 'That information is incorrect, try again';
exit();
}
从 MySQL 表中实际抓取数据
$id1 = $_GET['id'];
//check user exists
$check = mysql_query("SELECT * FROM `users` WHERE id='$id1'");
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$firstname = $get['first_name'];