我登录的加入表中的会话问题

时间:2013-10-01 06:46:54

标签: php mysql session

这张照片是我的2张桌子和列, 我想说:
成员中的“名字”为 $ username
帐户中的“密码”为 $ password
我的登录面板。

这是我的表格和专栏:

enter image description here


我的问题是这个,我不知道如果我加入了表格,我可以在会议中加入什么

if($countacc==1){
$_SESSION['-------this is my problem---------']=$rowacc[0];
$_SESSION['-------this is my problem---------']=$rowacc[1];
$_SESSION['-------this is my problem---------']=$rowacc[2];
$_SESSION['-------this is my problem---------']=$rowacc[3];
header("location: index.php");
}

这是我的完整代码:

<?
session_start();
include "connector.php";

$username=mysql_real_escape_string($_POST['user']);
$password=mysql_real_escape_string($_POST['pass']);

$sqlacc="SELECT * FROM members INNER JOIN accounts ON 
account.mem_id =members.mem_id WHERE members.firstname='$username' and accounts.password='$password'";

$resultacc = mysql_query($sqlacc);
$countacc = mysql_num_rows($resultacc);
$rowacc = mysql_fetch_array($resultacc, MYSQL_NUM);

if($countacc==1){
$_SESSION['-------this is my problem---------']=$rowacc[0];
$_SESSION['-------this is my problem---------']=$rowacc[1];
$_SESSION['-------this is my problem---------']=$rowacc[2];
$_SESSION['-------this is my problem---------']=$rowacc[3];
header("location:content/index.php");
}
else{
header("location:login.php");
}
?>

对不起,如果我的英语语法错了。

4 个答案:

答案 0 :(得分:1)

你可以这样做

session_start();
include "connector.php";

$username   =   mysql_real_escape_string($_POST['user']);
$password   =   mysql_real_escape_string($_POST['pass']);

$sqlacc="
        SELECT 
            members.id AS id
            members.firstname AS username,
            accounts.password AS password
        FROM members 
        INNER JOIN accounts ON account.mem_id =members.mem_id 
        WHERE members.firstname='$username' 
        AND accounts.password='$password'";

$resultacc  =   mysql_query($sqlacc);
$countacc   =   mysql_num_rows($resultacc);
$rowacc     =   mysql_fetch_assoc($resultacc);

if($countacc    ==  1){

    $_SESSION['id']         =   $rowacc['id'];
    $_SESSION['username']   =   $rowacc['username'];
    $_SESSION['password']   =   $rowacc['password'];

    header("location:content/index.php");
}
else{
    header("location:login.php");
}

在您的查询中为列提供别名 而不是使用mysql_fetch_array使用mysql_fetch_assoc 您还应该注意到mysql函数的使用被删除了。你应该使用mysqli函数。

答案 1 :(得分:0)

$_SESSION['your session name its anything what you want']=$rowacc[0];

装置

$_SESSION['username']=$rowacc[0];

并随时随地使用它。

,例如

if(isset($_SESSION['username']))
{
  //your code
}

会话示例: Introduction To PHP Sessions

session in php.net

答案 2 :(得分:0)

这是一种更好的方式

$rowacc = mysql_fetch_array($resultacc, MYSQL_ASSOC);

所以现在你可以做到

if($countacc==1){
    $_SESSION['username']=$rowacc['firstname'];
    $_SESSION['password']=$rowacc['password'];
    header("location:content/index.php");
      }

你可以放任何你想要的东西$ _SESSION ['here']。它只是让你可以记住并用于后者。其余部分与你的代码相同。

答案 3 :(得分:0)

$_SESSION是一个数组

index是用户定义的

所以添加一个索引$_SESSION["myIndex"]你要将项目添加到数组$_SESSION

将值指定为

$_SESSION["myIndex"] = "My Value";

并将其用作

echo $_SESSION["myIndex"];

输出:My Value

在使用session_start();

之前,

会话需要启动$_SESSION

要了解详情,请参阅$_SESSION manual