PHP RESt API for User Management App

In this article, we are going to learn how to make user registration API in PHP in an easy way.

Hope so you are interested to know how to make the user registration API using PHP, so now without wasting much time let’s start.

You can also refer our video tutorial for same.

So let’s start with this tutorial :

PHP RESt API for User Management App

1. First, we make the connection API to make a connection with the local database. So this is the code of connection API.

Connection API

<?php

   $hostName='localhost';
   $userName='root';
   $userPass='';
   $dbName='userdata';

   $con=mysqli_connect($hostName,$userName,$userPass,$dbName);

   /*if(!$con){

   	echo "connection failed";
   }
   else
   	echo "connection succes";*/

?>s

2. Now we Let’s create the Registration API in which we include the connection API. So the code of Register API is:

Register API

  <?php

  require 'connection.php';
  $username=$_POST['username'];
  $email=$_POST['email'];
  $password=md5($_POST['password']);



  $checkUser="SELECT * from user WHERE email='$email'";
  $checkQuery=mysqli_query($con,$checkUser);

  if(mysqli_num_rows($checkQuery)>0){

     $response['error']="002";
    $response['message']="User exist";
  }
  else
  {
     $insertQuery="INSERT INTO user(username,email,password) VALUES('$username','$email','$password')";
  $result=mysqli_query($con,$insertQuery);

  if($result){

    $response['error']="000";
    $response['message']="Register successful!";
  }
  else
  {
    $response['error']="001";
    $response['message']="Registeration failed!";
  }

  }

  
  echo json_encode($response);

?>

Login API

<?php

  require 'connection.php';

  $email=$_POST['email'];
  $password=md5($_POST['password']);

  $checkUser="SELECT * FROM user WHERE email='$email'";

  $result=mysqli_query($con,$checkUser);



  if(mysqli_num_rows($result)>0){ 

    $checkUserquery="SELECT id, username, email FROM user WHERE email='$email' and password='$password'";
    $resultant=mysqli_query($con,$checkUserquery);

    if(mysqli_num_rows($resultant)>0){

      while($row=$resultant->fetch_assoc())
      
      $response['user']=$row;
      $response['error']="200";
      $response['message']="login success";
    }
    else{
      $response['user']=(object)[];
      $response['error']="400";
      $response['message']="Wrong credentials";

    }
   
    
  }
  else{

    $response['user']=(object)[];
    $response['error']="400";
  	$response['message']="user does not exist";


  }

  echo json_encode($response);
    
?>

FetchAll User API

<?php

  require 'connection.php';

  $users="SELECT id, username, email FROM user";
  $result=mysqli_query($con,$users);


  if(mysqli_num_rows($result)>0){


    while($row=$result->fetch_assoc()){

      $response['users'][]=$row;
      $response['error']="200";
    }
  }
  else{

    $response['error']="400";
    $response['users'][]="";


  }

  
  echo json_encode($response);
    
?>

Update user API

<?php 

  	require 'connection.php';

    $id=$_POST['id'];
    $username=$_POST['username'];
    $email=$_POST['email'];

    $update_query="UPDATE user SET username='$username', email='$email' WHERE id='$id'";
    $result=mysqli_query($con,$update_query);
    

    if($result>0){

      $fetchuser=mysqli_query($con,"SELECT id, username, email FROM user WHERE email='$email'");

    if(mysqli_num_rows($fetchuser)>0){

      while($row=$fetchuser->fetch_assoc()){
        $response["user"]=$row;
        }
        $response['error']="200";
        $response['message']="user update success";
      }
    else{
      $response["user"]=(object)[];
      $response['error']="400";
      $response['message']="user update but detail not fetch";

    }

    }
    else{
      $response["user"]=(object)[];
      $response['error']="400";
      $response['message']="user update failed";
    }
  	


  	echo json_encode($response);

 ?>

UpdatePassword API

<?php  

	require 'connection.php';
	$email=$_POST['email'];
	$current=md5($_POST['current']);
	$new=md5($_POST['new']);
	

	$checkuser="SELECT * FROM user WHERE email='$email' and password='$current'";
	
	$result=mysqli_query($con,$checkuser);


	if(mysqli_num_rows($result)>0){

		$updatePass=mysqli_query($con,"UPDATE user SET password='$new' WHERE email='$email'");

		if($updatePass>0){

			$response['error']="200";
			$response['message']="password update success";

		}
		else{
			$response['error']="400";
			$response['message']="password not updated";

		}

	}
	else{
		$response['error']="400";
		$response['message']="User does not exist";

	}

	echo json_encode($response);

?>

Delete user Account API

<?php 
 	

 	require 'connection.php';
 	$id=$_POST['id'];

 	$deleteuser=mysqli_query($con,"DELETE FROM user WHERE id='$id'");

 	if($deleteuser>0){

 		$response['error']=200;
 		$response['message']="Account has been deleted";
 	}
 	else{

 		$response['error']=400;
 		$response['message']="Account has not been deleted";
 	}

 	echo json_encode($response);
 ?>

If you found this post useful, don’t forget to share this with your friends, and if you have any query feel free to comment it in the comment section.

Thank you 🙂 Keep Learning !

Leave a Comment

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