dqstti8945 2015-02-28 15:11
浏览 13
已采纳

外键不等于主键

So my query looks like this

`$dbUser = "SELECT * FROM accaount, role WHERE Username = :username AND Password = :password UNION SELECT * FROM role , accaount WHERE Role_idRole = idRole ";

$query = $conn->prepare( $dbUser);
$query->bindParam(":username", $Username);
$query->bindParam(":password", $Password);
$query->execute();`

The mistake is UNION SELECT * FROM role , accaount WHERE Role_idRole = idRole. what I try to do is to make my foreign key (Role_idRole) equal to my primary key from anothor table called role with the primary key of idRole. What i try to do is to look if your account is an admin account or not . so if i am an admin then Name = 1 of not Name = 0. Name is in the table from role.

Here is my full code for login

`<?php
if(!isset($_SESSION))
{
    session_start();
}
require('dbConnection.php');

    $Password = $_POST['LPassword'];
    $Username = $_POST['LUsername'];

    $dbUser = "SELECT * FROM accaount, role WHERE Username = :username AND Password = :password UNION SELECT * FROM role , accaount WHERE Role_idRole = idRole ";

    $query = $conn->prepare( $dbUser);
    $query->bindParam(":username", $Username);
    $query->bindParam(":password", $Password);
    $query->execute();

    $result = $query->fetch(PDO::FETCH_ASSOC);

    var_dump($result);

    if ($result["Username"] == $Username && $result["Password"] == $Password) {
        if($result['Name'] == 0) {
            $_SESSION['LOGINUsername'] = $Username;
            echo "Member";
        }
        $tpl->assign('USERNAME', $Username);
        if($result['Name'] == 1) {

            $_SESSION['LOGINAdmin'] = $Username;
            echo "Admin";
        }
    }
    else {
        echo "FAIL!";
}`

And I know account is misspelled but thats not the mistake its also like that in my database. also this is what var dump shows

array (size=8)

'idAccaount' => int 2

'Username' => string 'rwere' (length=5)

'Password' => string 'rwere' (length=5)

'salt' => string '380' (length=3)

'Reset' => null

'Role_idRole' => string '45' (length=2)

'idRole' => string '40' (length=2)

'Name' => int 1

as you can see this person had to be a member not a admin. Name int had to be 0 if we look at idRole 45.

I am really confused.

  • 写回答

1条回答 默认 最新

  • dongzhong1891 2015-03-04 22:59
    关注
    $dbUser = "SELECT accaount.Username, accaount.Password, accaount.Role_idRole, role.Name FROM accaount INNER JOIN role ON accaount.Role_idRole=role.idRole WHERE accaount.Username = :username AND accaount.Password = :password AND Role_idRole = idRole";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容