将db connexion实例传递给其他类php

时间:2017-03-31 00:11:28

标签: php mysql oop

我正在尝试使用PHP OOP创建一个注册系统,但是当我执行它时我得到了这个错误

  

注意:未定义的变量:在第14行的C:\ wamp64 \ www \ news \ classes \ user.php中连接

     

致命错误:在第14行的C:\ wamp64 \ www \ news \ classes \ user.php中调用null上的成员函数prepare()

我相信我的问题是我的calsse无法获得数据库连接实例,即使我在通知中传递它。我有2个班级和一个注册页面 我的第一堂课是dbconnect.php

<?php

    include "./config.php";

    class dbconnect{

        public  function __construct(){
            try {
                $conect = new PDO('mysql:host='.SERVER.'; dbname='.DATABASE, USERNAME, PASSWORD); 
                $conect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $conect;

            }
        catch(PDOException $e)
                {
                echo "Connection failed: " . $e->getMessage();
            }
        }

    }


?>

我的第二堂课是user.php

<?php

    class user{
        public  $username;
        public  $connect;

            function __construct($username,$connect){
                $this->username=$username;
                $this->connect=$connect;
            }

            protected function user_exist(){
                $request="SELECT * FROM `users` where username='".$this->username."'";
                $sql=$connect->prepare($request);
                $sql->execute();
                $results = $sql->fetch(PDO::FETCH_ASSOC);

                if(is_array($results)){
                    //echo "utilisateur exist";
                    return true;
                }
                else{
                    //echo "utilisateur n'existe pas";
                    return false;
                }           
            }
            public function register($firstname,$lastname,$email,$user,$password){
                if(!($this->user_exist())){
                    $request="INSERT INTO USERS (nom,prenom,email,user,pass,role,statut,date ) VALUES (:firstname, :lastname, :email, :username, :password, :role,:staut, :now())";
                    $sql = $connect->prepare($request);
                    $sql->bindParam(':fistname', $firstname);
                    $sql->bindParam(':lastname', $lastname);
                    $sql->bindParam(':email', $email);
                    $sql->bindParam(':username', $username);
                    $sql->bindParam(':password', $password);
                    $sql->bindParam(':role', 'subscriber');
                    $sql->bindParam(':statut', 'waiting for validation');
                    //$sql->bindParam(':date', $date);
                    try{
                        $sql->execute();
                    }
                    catch(Exception $e){
                        if ($e->getCode() == 1062) {
                            echo $e;
                        } else {
                            throw $e;
                        }
                    }

                }
            }


    } 

?>

然后是我的注册页面

<?php
    include "classes/dbconnect.php";
    include "classes/user.php";

    if((isset($_POST["submit"]))    and $_POST["verificaation"]=="submit1"){
        $nom        =   $_POST["nom"];
        $prenom     =   $_POST["prenom"];
        $email      =   $_POST["email"];
        $username   =   $_POST["username"];
        $pass       =   $_POST["password"];
        $passver    =   $_POST["passver"];
        $conect=new dbconnect();

        $user= new user($username,$conect->connect);
        $msg=$user->register($nom,$prenom,$email,$user,$pass);
        print_r ($conect);


    }




?>
<form method="post" action="register.php">
<label>Nom</label>
<input type="text" name="nom"/><br />
<label>Prenom</label>
<input type="text" name="prenom"/><br />
<label>Email</label>
<input type="email" name="email"/><br />
<label>Nom d'utilisateur</label>
<input type="text" name="username" /><br />
<label>Mot de passe</label>
<input type="password" name="password" /><br />
<label>Recrivez le mot de passe</label>
<input type="password" name="passver"/><br />
<input type="submit" name="submit" value="submit" /><br />
<input type="hidden" name="verificaation" value="submit1" />


</form>

1 个答案:

答案 0 :(得分:1)

在User.php上

,而不是使用:

$sql=$connect->prepare($request);

使用:

$sql=$this->connect->prepare($request);

此外,而不是使用:

$sql = $connect->prepare($request);

使用:

$sql = $this->connect->prepare($request);