代码之家  ›  专栏  ›  技术社区  ›  Domica

Php oop无法从db获取数据

php
  •  0
  • Domica  · 技术社区  · 6 年前

    所以我有一段代码,我只是按照一些指南来创建,

    <?php
    
    session_start();
    
    if (isset($_POST['submit'])) {
    
    include 'db.conf.php';
    
    $uid = mysqli_real_escape_string($conn, $_POST['uid']);
    $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
    $_SESSION['uid'] = $uid;
    
    //Error handleri
    //Check jesu inputi empty
    
    if (empty($uid) || empty($pwd))
    {
        header("Location: ../index.php?login=empty");
        exit();
    }
    else
    {
        $sql = "SELECT * FROM users WHERE user_uid = '$uid' OR user_email = '$uid'";
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);
    
        if ($resultCheck < 1)
        {
            header("Location: ../index.php?login=usernamenepostoji");
            exit();
        }
        else
        {
            if ($row = mysqli_fetch_assoc($result)) {
                //Dehashiranje
                $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
                if ($hashedPwdCheck == false) {
                    header("Location: ../index.php?login=invalidpass");
                    exit();
                }
                elseif ($hashedPwdCheck == true)
                {
                    //Logiranje
                    $_SESSION['u_id'] = $row['user_id'];
                    $_SESSION['u_first'] = $row['user_first'];
                    $_SESSION['u_last'] = $row['user_last'];
                    $_SESSION['u_email'] = $row['user_email'];
                    $_SESSION['u_uid'] = $row['user_uid'];
                    header("Location: ../index.php?login=success");
                    exit();
                }
    
            }
        }
    }
    
    }
    
    else
    {
        header("Location: ../index.php?login=error");
        exit();
    }
    
    ?>
    

    它只是简单的错误处理和登录工作。我理解它,想用更多的oop来重新创建它。

    <?php 
    session_start();
    
    include 'db.conf.php';
    
    class Login
    {
    
    public $username;
    public $password;
    
    
    function __construct()
    {
        $this->username = $_POST['uid'];
        $this->password = $_POST['pwd'];
        $this->checkinputs();
    }
    
    
    function checkinputs()
    {       
            if (empty($this->username) || empty($this->password)) 
            {
                header("Location: ../index.php?login=empty");
                exit();
            }
            else
            {   
                $username = $this->username;
                $sql = "SELECT * FROM users WHERE user_uid =".$username;
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);
    
                if ($resultCheck < 1) 
                {
                    header("Location: ../index.php?login=usernamenepostoji");
                    exit();
                }
                else 
                {
                if ($row = mysqli_fetch_assoc($result)) {
                    //Dehashiranje
                    $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
                    if ($hashedPwdCheck == false) {
                        header("Location: ../index.php?login=invalidpass");
                        exit();
                    }
                    elseif ($hashedPwdCheck == true) 
                    {
                        //Logiranje
                        $_SESSION['u_id'] = $row['user_id'];
                        $_SESSION['u_first'] = $row['user_first'];
                        $_SESSION['u_last'] = $row['user_last'];
                        $_SESSION['u_email'] = $row['user_email'];
                        $_SESSION['u_uid'] = $row['user_uid'];
                        header("Location: ../index.php?login=success");
                        exit();
                    }
                }
            }
        }
    }
    }
    
    
    ?>
    

    这就是我得到的,它是字面上相同的代码只是使用函数和其他东西来'分离'成块。它不起作用。我一直在if$resultCheck<1头上卡住,这意味着用户名不存在。不过我肯定是的,因为数据库里什么都没变。所以它让我想到是$conn,它只是没有连接到数据库。我已经将$username变量转储到一个文件中,以检查它是否有效。我只是不知道该怎么办。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Jeff    6 年前

    $conn 方法中不存在 checkinputs()

    要么全球化:

    function checkinputs()
    { 
        global $conn;
        ...
    }
    

    我不推荐(因为 using globals is disencouraged

    或者把它传给 Login::_construct() $this->conn (然后用作 $this->连接 $result = mysqli_query($this->conn, $sql); ):

    function __construct($conn)
    {
        $this->conn = $conn; // maybe also check if you have a valid connection!
        $this->username = $_POST['uid'];
        $this->password = $_POST['pwd'];
        $this->checkinputs();
    }
    
    function checkinputs()
    {       
    // no global now!
            ....    
            $result = mysqli_query($this->conn, $sql);
            ....
    }
    

    但是 :请切换到 prepared stements . 此代码易受sql注入攻击!

    Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?