-1

I have prepared a structure to manage the application into OOPS Manner in php, i am trying to connect database and insert the records into database, my connection is established but unable to add records into my DB-tables. Here is my codes My Model User class file: users.php (Which is a folder name class):

<?php
class Users extends Connection{

    public $con;
    public function __construct(){
        $this->con = new Connection();
    }
    //creating user
    public function createInfo($data){
        $username = $data['username'];
        $email = $data['useremail'];
        $password = md5($data ['password']);
        $query = "INSERT INTO js_users(username, useremail, `password`) VALUES ('".$username."', '".$email."', '".$password."')";
        $result = $this->con->query($query); 
        print_r($result); exit;
        return array("status"=>$result, "message"=>"User Generated Successfully");
     }
     public function getUserInfo(){
         $query  = "SELECT * FROM employee";
         $result = mysqli_query($this->con, $query) or die(mysqli_error($this->con));
         return $result;
     }  
}

My Model: Connection class file (Same in class folder):

<?php

class Connection{

    private $hostname;
    private $username;
    private $password;
    protected $dbname;
    public $con;
    public function __construct()
    {   
        $this->hostname = '127.0.0.1';
        $this->username = 'localuser';
        $this->password = 'R@5gull@';
        $this->dbname = 'jobsearch';
        $this->con = $this->connect();
        return $this->con;
    }
    private function connect(){
        $this->con =  new mysqli($this->hostname, $this->username, $this->password, $this->dbname);
        return $this->con;   
    }

}
?>

My HTML Form : (In root directory):

 <form class="user" action="process.php" method="post">
              <input type="hidden" name="actions" value="createUser" />

                <div class="form-group row">
                  <div class="col-sm-10 mb-3 mb-sm-0">
                    <input type="text" class="form-control form-control-user" id="username" name="data[username]" placeholder="Choose username">
                  </div>
                </div>
                <div class="form-group row">
                <div class="col-sm-10 mb-3 mb-sm-0">
                  <input type="email" class="form-control form-control-user" id="useremail" name="data[useremail]" placeholder="Email Address">
                </div>
                </div>
                <div class="form-group row">
                  <div class="col-sm-5 mb-3 mb-sm-0">
                    <input type="password" class="form-control form-control-user" id="password" name="data[password]" placeholder="Password">
                  </div>
                  <div class="col-sm-5">
                    <input type="password" class="form-control form-control-user" id="confirm_password" name="data[confirmPassword]" placeholder="Repeat Password">
                  </div> 
                  <div class="col-sm-2">
                  <span id="message"></span>
                  </div>

                </div>
                <button type="submit" name="submit"  class="btn btn-primary btn-user btn-block">
                  Register User
                </button>
              </form>

My Process.php:

<?php
function __autoload($class){
    require_once "class/$class.php";
}


$connect = new Connection();
$action = $_POST['actions'];
if(isset($action) && isset($_POST['actions'])){
switch($action){
    case "createUser":
        $createUser = new Users();
        $res = $createUser->createInfo($_POST['data']);
        print_r($res);
    break;

    default:
        return NULL;
    }
}
?>

my database schema:

CREATE TABLE `js_users` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(155) NULL DEFAULT NULL,
    `useremail` VARCHAR(155) NULL DEFAULT NULL,
    `password` VARCHAR(255) NULL DEFAULT NULL,
    `created_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_on` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `username` (`username`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;

As my connection has establish but records are not being pushed to the database, want to insert records via these methods.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Manish Agarwal
  • 166
  • 2
  • 12
  • print_r($result); exit; what does this prints ? – alwaysLearn Jul 03 '19 at 06:27
  • I've got some useful articles for you, [How to connect using mysqli properly](https://phpdelusions.net/mysqli/mysqli_connect) and [Your first database wrapper childhood diseases](https://phpdelusions.net/pdo/common_mistakes) – Your Common Sense Jul 03 '19 at 07:21

1 Answers1

2

In your Connection class, the only thing it does is establish a connection and store this in $this->con (you cannot return anything from a constructor).

Your user class definition uses...

class Users extends Connection

which is not good OO design as a User isn't a type of database connection. So I would suggest dropping the extends Connection bit - you don't use it anyway.

But then in your constructor you use

$this->con = new Connection();

so $this->con is now an instance of the connection class. Which isn't an actual database connection, but a class which contains one. When you run the query

$result = $this->con->query($query); 

I would expect this to fail as the Connection class doesn't contain a method called query(). With the current code, you could do it with...

$result = $this->con->con->query($query); 

As $con is a public property of the class and contains the actual class.

There are several other issues - please use prepared statements for any SQL where user input is part of the statement. Don't use md5() for passwords - have a look into password_hash(). Also look into dependency injection to pass the database into any instance using one as this allows better control and testing.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55