OOP PHP Registration and Login - Part 1

In this tutorial we will create a simple registration form in an Object Oriented Programming(OOP) concepts. The Object Oriented Programming is a programming language model organised in an objected paradigm, in which it is not only data type, but also types of operational functions that can apply to the data structure.Now that you understand about OOP, So now lets take a look and start coding. Database - This is where you store the value of an input type First we will create our database: * Open your web server and type "localhost/phpmyadmin" in the url * After that create your database and name it "oop", then after that click SQL and just copy/paste the code below
  1. CREATE TABLE `user` (
  2. `user_id` int(11) NOT NULL,
  3. `username` varchar(20) NOT NULL,
  4. `password` varchar(12) NOT NULL,
  5. `firstname` varchar(30) NOT NULL,
  6. `lastname` varchar(30) NOT NULL
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
config.php This is where the database server is being assigned to its specific property
  1. <?php
  2. define('db_host', 'localhost');
  3. define('db_user', 'root');
  4. define('db_pass', '');
  5. define('db_name', 'oop');
  6. ?>
class.php This is the main PHP file that holds the operation of the database such as(connection data, insert etc...)
  1. <?php
  2. require 'config.php';
  3.  
  4. class db_class{
  5. public $host = db_host;
  6. public $user = db_user;
  7. public $pass = db_pass;
  8. public $dbname = db_name;
  9. public $conn;
  10. public $error;
  11.  
  12. public function __construct(){
  13. $this->connect();
  14. }
  15.  
  16. private function connect(){
  17. $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
  18. if(!$this->conn){
  19. $this->error = "Fatal Error: Can't connect to database".$this->conn->connect_error;
  20. return false;
  21. }
  22. }
  23.  
  24. public function save($username, $password, $firstname, $lastname){
  25. $stmt = $this->conn->prepare("INSERT INTO `user` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)") or die($this->conn->error);
  26. $stmt->bind_param("ssss", $username, $password, $firstname, $lastname);
  27. if($stmt->execute()){
  28. $stmt->close();
  29. $this->conn->close();
  30. return true;
  31. }
  32. }
  33. }
  34. ?>
Creating Mark Language index.php The code below contains all the field that use in storing the data to the database server,
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <meta charset = "UTF-8" name "viewport" content = "width-device=width, initial-scale=1"/>
  5. <title>OOP PHP Registrarion and Login Form Using MySQLi</title>
  6. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  7. </head>
  8. <body>
  9. <nav class = "navbar navbar-default">
  10. <div class = "container-fluid">
  11. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <br />
  15. <br />
  16. <br />
  17. <div class = "row">
  18. <div class = "col-md-4">
  19. </div>
  20. <div class = "col-md-4 well">
  21. <h4 class = "text-danger">OOP PHP Registration and Login Form Using MySQLi</h4>
  22. <hr style = "border-top:1px dotted #000;"/>
  23. <form method = "POST" action = "save_query.php">
  24. <div class="form-group">
  25. <input type = "text" placeholder = "Username" name = "username" class = "form-control"/>
  26. </div>
  27. <div class="form-group">
  28. <input type = "password" placeholder = "Password" name = "password" class = "form-control">
  29. </div>
  30. <div class="form-group">
  31. <input type = "text" placeholder = "Firstname" name = "firstname" class = "form-control"/>
  32. </div>
  33. <div class="form-group">
  34. <input type = "text" placeholder = "Lastname" name = "lastname" class = "form-control"/>
  35. </div>
  36. <button class = "btn btn-success" name = "signup"><span class = "glyphicon glyphicon-log-in"></span> Sign up</button>
  37. </form>
  38. </div>
  39. </div>
  40. </body>
  41. </html>
Creating Insert Script save_query.php This is where the input value is being stored into a variable, and later will be send to the main PHP file by calling the save() function
  1. <?php
  2. require_once "class.php";
  3. $conn = new db_class();
  4. if(ISSET($_POST['signup'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $firstname = $_POST['firstname'];
  8. $lastname = $_POST['lastname'];
  9. $conn->save($username, $password, $firstname, $lastname);
  10. echo '<script>alert("Successfully saved!")</script>';
  11. echo '<script>window.location= "index.php"</script>';
  12. }
  13. ?>
There you have it we created the simple registration using OOP PHP. I hope that this tutorial help you to your project. On my next tutorial we will create the login form based on the following code that I have given, so just stay tuned to this site. For more questions, just comment below. Enjoy Coding!

Tags

Comments

Submitted bybahrulon Wed, 01/18/2017 - 10:31

where the project part 2?

Add new comment