Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 登录尝试将变量传递到登录页_Php_Mysql_Pdo - Fatal编程技术网

Php 登录尝试将变量传递到登录页

Php 登录尝试将变量传递到登录页,php,mysql,pdo,Php,Mysql,Pdo,我将我的登录页面改为使用PDO。经过研究,我似乎找不到一个解决方案,如何传递登录页面中未定义的变量。我可以传递用户名并显示它,没有任何问题,但是无论我尝试什么,memberid和firstname都不会传递 我正在尝试传递id和firstname-以显示名字。id用于定义登录用户,因为数据将使用该用户的现有信息填充数据库 有两个数据库,一个包含注册信息-id、firstname、lastname、password….. 第二个数据库将填充考试信息,并将再次与注册数据库链接,以便它知道谁在参加考试

我将我的登录页面改为使用PDO。经过研究,我似乎找不到一个解决方案,如何传递登录页面中未定义的变量。我可以传递用户名并显示它,没有任何问题,但是无论我尝试什么,memberid和firstname都不会传递

  • 我正在尝试传递id和firstname-以显示名字。id用于定义登录用户,因为数据将使用该用户的现有信息填充数据库

  • 有两个数据库,一个包含注册信息-id、firstname、lastname、password…..
    第二个数据库将填充考试信息,并将再次与注册数据库链接,以便它知道谁在参加考试

  • 下面是我试图将所需变量传递到登录页减去html的代码

    LOGIN.php
        require_once('inc/config.php');
    
        //check if already logged in move to home page
        if( $user->is_logged_in() ){ header('Location: members.php'); } 
    
        //process login form if submitted
        if(isset($_POST['submit'])){
            $memberID = $_POST['memberID'];
            $firstname= $_POST['firstname'];
            $email = $_POST['email'];
            $password = $_POST['password'];
    
            if($user->login($email,$password)){ 
                $_SESSION['email'] = $email;
                $_SESSION['memberID'] = $memberID;
                $_SESSION['firstname'] = $firstname;
                header('Location: members.php');
                exit;
    
            } else {
                $error[] = 'Wrong email or password.';
            }
    
        }
    
       ?>
    
    USER.php我在这里添加了memberid和firstname,但没有传递

    <?php
    include('password.php');
    class User extends Password{
    
        private $_db;
    
        function __construct($db){
            parent::__construct();
    
            $this->_db = $db;
        }
    
        private function get_user_hash($username){  
    
            try {
                $stmt = $this->_db->prepare('SELECT memberID, firstname, password FROM members WHERE username = :username AND active="Yes WHERE memberID = :memberID AND firstname = :firstname LIMIT 1" ');
    
    
                $stmt->execute(array('memberID' => $memberID));
                $stmt->execute(array('firstname' => $firstname));
                $stmt->execute(array('username' => $username));
    
                $row = $stmt->fetch();
                return $row['password'];
    
            } catch(PDOException $e) {
                echo '<p class="bg-danger">'.$e->getMessage().'</p>';
            }
        }
    
        public function login($username,$password){
    
            $hashed = $this->get_user_hash($username);
    
            if($this->password_verify($password,$hashed) == 1){
    
                $_SESSION['loggedin'] = true;
                return true;
            }   
        }
    
        public function logout(){
            session_destroy();
        }
    
        public function is_logged_in(){
            if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
                return true;
            }       
        }
    
    }
    
    
    ?>
    

    我看到的第一件事是您没有将它们传递到
    get\u user\u hash
    这意味着它们不存在于该函数中,即使它们具有相同的名称

    谢谢b729sefc。你的意思是这样的-私有函数get\u user\u hash($username、$memberID、$firstname)?当我这样做时,我得到-SQLSTATE[HY093]:无效的参数号:未定义参数。然后我添加了-$stmt->execute(array('memberID'=>$memberID))$stmt->execute(数组('firstname'=>$firstname));但是我收到了相同的消息-SQLSTATE[HY093]:无效的参数号:未定义参数。您还需要在登录函数中传递它们。正确,我已经这样做了-如果($user->login($email,$password,$memberID,$firstname)){$\u会话['email']=$email;$\u会话['memberID']=$memberID;$\u会话['firstname']=$firstname;其他人知道为什么变量没有传递吗?如果你们不知道,把mysqli放回去。
    <?php
    session_start();
     $_SESSION['memberID'] = $memberID['memberID'];
     $_SESSION['firstname'] = $firstname['firstname'];
    ?>
    
    <?php echo $_SESSION['memberID']; ?>
    <?php echo $_SESSION['firstname']; ?>