How to Create simple register and login page with PHP-OOP and MySQL | Part 1.

How to Create simple register and login page with PHP-OOP and MySQL | Part 1.

In this tutorial, we are going to create a register page that allows user to enter data and submit to MySQL database with PHP as our back-end. This is a continuation of our prior article where we created a register page with HTML. If you don’t know how to create a registration form I prefer you go back and have a look at our post How to Create HTML Login Page With Bootstrap.

We are going to use a class approach in this article, am hoping that you have prior knowledge of classes, objects, functions and constructors.

First of all, we are going to create a database called test and a table called test where we will save our data. Copy and paste the codes below to create your database and table user.

CREATE DATABASE test;
CREATE TABLE `user` (
`Full_Name` varchar(50) NOT NULL,
`Username` varchar(20) NOT NULL,
`Password` varchar(20) NOT NULL
)

Next we are going to create a register form. Create a file called index.php. Copy and paste the code below in index.php.

<?php
?>
<html>
<header>
<title>Register and Login Page With PHP OOP and Html Part 1</title>
<link rel="stylesheet" href="CSS/bootstrap.min.css">
</header>
<body>
<div class="container">
<h3 class="text-center text-info">How To CReate Register and Login Form With PHP-OOP and MySQL PART 1</h1>
<hr>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="well">
<form class="form-group" action="register_query.php" method="post">
<label>Full-Name</label>
<input type="text" name="f_name" class="form-control">
<label>Username</label>
<input type="text" name="user_name" class="form-control">
<label>Password</label>
<input type="password" name="pass" class="form-control">
<button class="btn btn-block btn-success mt-2" name="submit">register</button>  
<p class="mt-2 text-center">Already registered? <a href="#">Click here</a> to login.</p>
</form>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
</body>
</html>

Now we have created our Register Form. Create two files; db_config.php and class.php. db_config file contains database configurations while class.php contains db_class that uses db_config file to initiate database connection.

Copy and paste the lines below to db_config.php. Replace the values below with ones on your server.

<?php 
define ('host', 'localhost');
define ('user', 'root');
define ('password', '');
define ('db_name', 'test');
?>

Lets now create our db_class by pasting the codes below in class.php.

<?php 
#we are going to import db_config file here since it contains all database configurations.text-center
#you can use include 'db_config' or one have used down here.
require 'db_config.php';
#Create class for database. 
#declare your variables as public variables for global accessibility.
class db_class{
public $host = host;
public $user = user;
public $password = password;
public $dbname = db_name;  
public $conn;
public $error; 
#This is a method that is used to access database configurations.
public function __construct() {
$this->connect();
}
#Function to create database connection to your database server. 
#We are making it private to prevent external access. 
private function connect(){
$this->conn =new mysqli($this->host, $this->user, $this->password, $this->dbname);
if (!$this->conn){
$this->error = "Fatal Error: Failed to connect to database". $this->conn->connect_error();
return false;
}
}
#This is a function that is going to submit your data into database after successful connection has been established.
#Make sure that data fields here correspond to the fields in your database.
public function register($f_name, $user_name, $pass){
$stmt = $this->conn->prepare("INSERT INTO `user` (Full_Name, Username, Password) 
VALUES (?, ?, ?)") or die($this->conn->error);
$stmt->bind_param("sss", $f_name, $user_name, $pass);
if($stmt->execute()){
$stmt->close();
$this->conn->close();
return true;
}
}
}
?>

Now try entering data in your form and click register to submit data in MySQL database.

Byee!!!

10 thoughts on “How to Create simple register and login page with PHP-OOP and MySQL | Part 1.

  1. Olá estou tãо feliz encontrei ѕeս blog рágina , realmente tе
    encontrei рor acidente, еnquanto eu estava pesquisando em AOL pаra outra coisa, Ꭰe qᥙalquer fօrma
    еu estou aգui agоra e gostaria de Ԁizer obrigado para um fantástico post e um
    ρara toԁօѕ interessante blog (еu também amo ο tema/design), nãߋ tenho
    tempo para browse isso tudo ao minutos mɑs
    tenhо salvou – е também incluído ѕeu RSS feeds ,
    então quando eս tenho tempo eu estarei volta
    para ler muіto mаis , por favor continuem
    a impressionante tгabalho.

  2. Does your site have a contact page? I’m having a tough time locating it but, I’d like
    to shoot you an e-mail. I’ve got some suggestions
    for your blog you might be interested in hearing. Either way, great site and I
    look forward to seeing it develop over time.

  3. Hello! I’ve been reading your website for some
    time now and finally got the courage to go ahead and give you a shout out from Humble Tx!
    Just wanted to say keep up the good job!

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen − 11 =