doukeng1922 2017-03-31 00:11
浏览 31
已采纳

将db connexion实例传递给其他类php [关闭]

i am trying to make a registration system using PHP OOP but im getting this errors when i excute it

Notice: Undefined variable: connect in C:\wamp64\www ews\classes\user.php on line 14

Fatal error: Call to a member function prepare() on null in C:\wamp64\www ews\classes\user.php on line 14

i beleive that my problem is that my calsse can't get the db connection instance even im passing it on intiation. i have 2 classes and a registration page my first class is 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();
            }
        }

    }


?>

my second class is 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;
                        }
                    }

                }
            }


    } 

?>

and then my registration page

<?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条回答 默认 最新

  • douxun2023 2017-03-31 11:03
    关注

    on User.php, Instead of using :

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

    Use :

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

    Also, Instead of using :

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

    Use :

    $sql = $this->connect->prepare($request);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?