0

I am learning oops concepts in php. I created Login/Signup/dbconfig pages using PHP PDO. DBconfig.php is working well but signup page gives below error.

Fatal error: Uncaught Error: Call to undefined method dBase::prepare() in D:\xampp\htdocs\phpoops\signup.php:26 Stack trace: #0 D:\xampp\htdocs\phpoops\signup.php(20): signUp->register() #1 D:\xampp\htdocs\phpoops\index.php(7): signUp->__construct(Object(dBase)) #2 {main} thrown in D:\xampp\htdocs\phpoops\signup.php on line 26

I created same code in procedural manner it works well. How can I solve this problem?

Here is my code:

Index.php

<?php  
    require("dbconfig.php");
    $db = new dBase();
    if(isset($_POST['submit'])){
    require_once("signup.php");
    $reg = new signUp($db);
    $reg->register();
   }
?>

<form action="" method="post">          
      <div class="top-row">
        <div class="field-wrap">
          <label>
            First Name<span class="req">*</span>
          </label>
          <input type="text" name="ufname" required autocomplete="off" />
        </div>        
        <div class="field-wrap">
          <label>
            Last Name<span class="req">*</span>
          </label>
          <input type="text" name="ulname" required autocomplete="off"/>
        </div>
      </div>
      <div class="field-wrap">
        <label>
          Email Address<span class="req">*</span>
        </label>
        <input type="email" name="uemail" required autocomplete="off"/>
      </div>          
      <div class="field-wrap">
        <label>
          Set A Password<span class="req">*</span>
        </label>
        <input type="password" name="upass" required autocomplete="off"/>
      </div>          
      <button type="submit" name="submit" class="button button-block"/>Get Started</button>          
      </form>

dbconfig.php

<?php 
class dBase
{
protected $uname;
protected $upass;
protected $host;
protected $dbname;
private   $dbcon;

function __construct()
{
     $this->dbcon  = false;
     $this->dbname = "oopsdb";
     $this->uname  = "root";
     $this->upass  = "";
     $this->host   = "localhost";
     //$this->connect();
     if(!$this->dbcon){
        try{
            $this->dbcon = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname.'',$this->uname,$this->upass);
            $this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e){
            echo $e->getMessage();
            die();
        }
    }
    return $this->dbcon;    
 }  
}

Signup.php

<?php 
   class signUp
  {
   private $fname;
    private $lname;
   private $email;
   private $pass;
    private $db;
   function __construct($db)
   {    
    $this->fname = htmlspecialchars($_POST['ufname']);
    $this->lname = htmlspecialchars($_POST['ulname']);
    $this->email = htmlspecialchars($_POST['uemail']);  
    $this->pass = htmlspecialchars($_POST['upass']);
    $this->db = $db;
    $this->register();
}

function register(){        
    if($_SERVER["REQUEST_METHOD"] == "POST"){
    $sql = "INSERT INTO user(`fname`,`lname`,`email`,`pass`,`usr_update`) VALUES (:fname,:lname,:email,:pass,NOW())";
    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(':fname',$this->fname);
    $stmt->bindParam(':lname',$this->lname);
    $stmt->bindParam(':email',$this->email);
    $stmt->bindParam(':pass',$this->pass);
    $stmt->execute();
    if($stmt == true){
        echo "<script>alert('Successfully Inserted');</script>";
    }else{
        echo "<script>alert('Not Inserted...please check logic once');</script>"; 
    }   

    }
  }
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
muraliniceguy97
  • 333
  • 1
  • 3
  • 14

1 Answers1

-1

I am learning oops concepts in php.

That's fine.
However, OOP is not stands for creating as much classes as possible. Create a new class only if you have an idea what it should be used for.
For the moment, your dBase class doesn't do anything that vanilla PDO can't do. So just use PDO instead.

dbconfig.php

 <?php

 $dbname = "oopsdb";
 $uname  = "root";
 $upass  = "";
 $host   = "localhost";
 $charset = "utf8";

 $db = new PDO("mysql:host=$host;dbname=$dbname;charset=$charset",$uname,$upass);
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

then remove $db = new dBase(); from index and keep the rest as is

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345