我正在尝试将查询结果中的值分配给会话,但它无法正常工作..
会话用户名 下面,我正在注册会话用户名,即用户电子邮件
$_SESSION['myusername'] = $myusername;
检查登录 在这里,我试图从同一个表中获取电子邮件存储的客户名称,并将其保存在“名称”会话中
$_SESSION['name'] = $row['name'];
记录成功 我在这里尝试获取用户名
echo "Welcome ". $_SESSION['name'];
检查登录页面*
<?php
ob_start();
$host="localhost"; // Host name
$username="xx"; // Mysql username
$password="xx"; // Mysql password
$db_name="xx"; // Database name
$tbl_name="customer"; // Table name
$db_usercol="email"; // Field containing username
$db_passcol="password"; // Field containing password
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE $db_usercol='$myusername' and $db_passcol='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "profile.php"
session_register("myusername");
session_register("mypassword");
$_SESSION['myusername'] = $myusername;
$_SESSION['name'] = $row['name'];
header("location:profile.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
成功登录*
<?
// Check if session is not registered , redirect back to main page.
// Put this code in first line of web page.
session_start();
if(isset($_SESSION[$myusername])){
header("location:members.php");
}
else
include("header.php");
?>
<body id="profile">
<div id="wholewrap">
<div id="bodywrap">
<div id="content">
<h1>Your Profile</h1>
<?php
echo "Welcome ". $_SESSION['myusername'];
echo "Welcome ". $_SESSION['name'];
?>
</div> <!-- content #end -->
<div id="sidebar">
Lorem Ipsum
</div> <!-- sidebar #end -->
</div> <!-- bodywrap #end -->
</div> <!-- wholewrap #end -->
<?php include("footer.php"); ?>
结果是
我只获取用户名而不是用户名,这是同一个名为 name
的表中的一个字段
答案 0 :(得分:1)
首先你必须有一个查询,类似这样的
$query = mysql_query("SELECT `name` FROM `users` WHERE `email`='".$_POST['email']."'") or die(mysql_error()); // of course you want to clear the strings and validate them
然后你做
if(mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
$_SESSION['name'] = $row['name'];
}