0

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

这是我的表格和列:

在此处输入图像描述


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

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

4 回答 4

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 函数。

于 2013-10-01T06:57:54.887 回答
0
$_SESSION['your session name its anything what you want']=$rowacc[0];

方法

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

并在任何你想要的地方使用它。

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

会话示例: PHP 会话简介

php.net 中的会话

于 2013-10-01T07:00:14.653 回答
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'] 。这只是为了让您记住并用于后者。其余部分与您的代码相同。

于 2013-10-01T07:02:00.413 回答
0

$_SESSION是一个数组

index是用户定义的

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

赋值为

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

并将其用作

echo $_SESSION["myIndex"];

输出:My Value

会话需要session_start();在使用前开始$_SESSION

要了解更多信息,请参阅$_SESSION 手册

于 2013-10-01T07:04:07.187 回答