Here I am going to build a simple Registration web service with the help of Php , MySQL and Json .

Create a Database named as Test :

At first we create a Table named as user having six Columns in Test Database.

Create a Database Connection :

At first I am Connecting Php with Mysql Database

Config.php

<?php
define('hostname','localhost');
 define('user','root');
 define('password','root');
 define('db_name','Test');
?>

Here MySQL database user name is root and password is root and database name is Test. Now we Create connection file to Connect  the MySQL database and php

Con.php

<?php
 require_once 'config.php';
 class Connection{
   private $connect;
   function __construct(){
   $this->connect=mysqli_connect(hostname,user,password,db_name) or die("DB Connection error.");
   }
   public function get_connection()
   {
       return $this->connect;
    }
 }
       
 ?>

After this we are going create an another file named as register.php

<?php
 include('con.php');
 header('Content-Type: application/json');
 /**
 User Registeration
 */
 class Register
 {
 private $db;
 private $connection;
 function __construct()
 {
 //constructor call
 $this->db = new Connection();
 $this->connection=$this->db->get_connection();
 }
 public function does_user_exist($name,$mail,$mobile,$add,$pass)
 {
 // does user already exist or not
 $query = "SELECT * FROM user WHERE Email='$mail'";
 $result=mysqli_query($this->connection,$query);
 if(mysqli_num_rows($result)>0){
$json['status']=400;
$json['message']=' Sorry '.$mail.' is already exist.';
   echo json_encode($json);
   mysqli_close($this->connection);
 }else {
   $query="insert into user_details(name,email,mobile,address,password) values('$name','$mail','$mobile','$add','$pass')";
   $is_inserted=mysqli_query($this->connection,$query);
   if($is_inserted == 1){
$json['status']=200;
     $json['message']='Account created, Welcome '.$name;
   }else {
$json['status']=401;
     $json['message']='Something wrong';
   }
   echo json_encode($json);
   mysqli_close($this->connection);
 }
 } 
 }
 $register=new Register();
 if(isset($_POST['name'],$_POST['email'],$_POST['mobile'],$_POST['address'],$_POST['password']))
 {
   $name=$_POST['name'];
   $mail=$_POST['email'];
   $mobile=$_POST['mobile'];
 $add=$_POST['address'];
   $pass=$_POST['password'];
 if (!empty($name) && !empty($mail) && !empty($mobile) && !empty($add) && !empty($pass)) {
     $encrypted_password=md5($pass);
     $register-> does_user_exist($name,$mail,$mobile,$add,$encrypted_password);
   }else {
$json['status']=100;
     $json['message']='You must fill all the fields';
     echo json_encode($json);
   }
 }

?>

Now we can test it in Postman or any other rest client with the given key parameters name , email , mobile , address and password . 🙂

%d bloggers like this: