-1

我在表 1 中有用户名,在表 2 中有密码。登录的语法是什么?

如果我有两个表,如何创建登录功能:

TABLE1 NAME: accounts | COLUMN: acc_id, password, mem_id
TABLE2 NAME: members  | COLUMN: mem_id, firstname

我想将“名字”设为 $username 并将“密码”设为 $password。我这里有代码,但我无法继续,因为我不知道它是否正确。

$sqlacc="SELECT * FROM members INNER JOIN accounts ON 
account.acc_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['idontknow']=$rowacc[0];
$_SESSION['idontknow']=$rowacc[1];
header("location:content/index.php");
}
4

2 回答 2

0

试试下面的代码

session_start();

$link = mysql_connect('localhost', 'mysql_username', 'mysql_password');

mysql_select_db('dbname', $link);

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

$sqlacc=" select * ".
        " from members m ".
        " inner join accounts a on a.mem_id=m.mem_id ".
        " where a.password='".$password."' and m.firstname='".$username."'";

$result = mysql_query($sqlacc);
$countacc = mysql_num_rows($result);

if($countacc==1){
    $rowacc = mysql_fetch_array($result);
    $_SESSION['memId']=$rowacc[0];
    $_SESSION['accId']=$rowacc[2];
    header("Location:content/index.php");
}

在您的查询中,加入条件是错误的

account.acc_id=members.mem_id

注意:您可以对任何两列执行连接,但条件是它们必须具有相同的数据类型和大小

于 2013-10-01T05:06:39.787 回答
0

尝试这个:

在您的查询连接条件错误映射(account.acc_id=members.mem_id

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


$sqlacc="SELECT * FROM members INNER JOIN accounts ON 
account.mem_id =members.mem_id WHERE members.firstname='$username' and accounts.password='$password'";
于 2013-10-01T05:07:26.737 回答