1

我正在尝试将查询结果中的值分配给会话,但它不起作用..

下面的会话用户名 ,我正在注册用户电子邮件的会话用户名

$_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的字段

结果

4

1 回答 1

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'];
}
于 2012-04-06T21:49:11.210 回答