-2

Here is my code, i am tryng to insert data into database using PDO, i don't know where my code went wrong that is not inserting and returning false on lastInsertId(). Have a look at my code:

Model.php

<?php
abstract class Model{
protected $dbh;
protected $stmt;

    public function __construct(){
        $this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
     }

     public function error(){
        print_r($this->dbh->errorInfo());
     }

     public function query($query){
       $this->stmt = $this->dbh->prepare($query);
     }

    //Binds the prep statement
    public function bind($param, $value, $type = null){
      if (is_null($type)) {
          switch (true) {
              case is_int($value):
                  $type = PDO::PARAM_INT;
                  break;
              case is_bool($value):
                  $type = PDO::PARAM_BOOL;
                  break;
              case is_null($value):
                  $type = PDO::PARAM_NULL;
                  break;
              default:
                  $type = PDO::PARAM_STR;
       }
      }
      $this->stmt->bindValue($param, $value, $type);
  }

   public function execute(){
      return $this->stmt->execute();
   }

    public function resultset(){
       $this->execute();
       return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function lastInsertId(){
        return $this->dbh->lastInsertId();
    }

    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    }

register function:(this is my register function where insert query is).

public function register(){

    $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
    $pass = md5($post['password']);
    if(isset($post['submit']))
    {
        $this->query('INSERT INTO tbl_users (firstname, lastname, email, password, gender, dob) VALUES(:firstname, :lastname, :email, :password, :gender, :dob)');
        $this->bind('firstname', $post['fname']);
        $this->bind('lastname', $post['lname']);
        $this->bind('email', $post['email']);
        $this->bind('password', $pass);
        $this->bind('gender', $post['gender']);
        $this->bind('dob', $post['date']);
        $this->execute();

        if($this->lastInsertId()){
            header('Location:'.ROOT_PATH.'admin/user');
        }else{
            echo "wrong";
            $this->error();
        }
    }
}

Can Somebody tell me where i did wrong, and why this is happening, Any help would be appreciated. Thank You in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • What is the value of `$this->lastInsertId()`? Zero? – Felippe Duarte Jul 05 '18 at 21:42
  • @FelippeDuarte it is resolved now. Thanks for participating. check my answer if you would want to know how. – Gurpreet Kaur Jul 05 '18 at 21:50
  • Why did you names you PDO wrapped as "Model"? Why are you using MD5 for hashing password? Why have you not looked up how to correctly set up PDO connection? – tereško Jul 06 '18 at 10:11
  • @tereško i am just learning, tell me how can i improve my code? – Gurpreet Kaur Jul 06 '18 at 21:15
  • @GurpreetKaur most important part: use https://secure.php.net/manual/en/book.password.php for hashing/verifying passwords. MD5 is **NOT** secure, because it was designed for speed. As for "model" part, [this post](https://stackoverflow.com/a/5864000/727208) might help. And regarding PDO setup: read http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – tereško Jul 06 '18 at 21:26
  • @tereško Thank You for your guidance. I will surely improve my code. :) – Gurpreet Kaur Jul 06 '18 at 22:07

1 Answers1

0

Issue is resolved. It was one of my database fields causing the issue. I set NULL equals to no and also wasn't giving any default value, which caused the error. I found out about it by replacing my code in model.php:

$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);

with this:

$options = array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
$this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS, $options);
Pang
  • 9,564
  • 146
  • 81
  • 122