0

i created web service for post data into Mysql. i want to design web service for android application,when i used postman rest Api i had this "Error Creating User" my files are here please check it for me. i complete the fields username,password and email in post man body but i don't know where the problem is.you can see the result in photo. attached image

<?php 
 define('DB_NAME','android');
 define('DB_USER','root');
 define('DB_PASSWORD','');
 define('DB_HOST','localhost');

 

<?php 

 class DbConnect{

  private $con; 

  function __construct(){

  }

  function connect(){
   include_once dirname(__FILE__).'/Constants.php';
   $this->con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

   if(mysqli_connect_errno()){
    echo "Failed to connect with database".mysqli_connect_err(); 
   }

   return $this->con; 
  }
 }

<?php 

 class DbOperations{

  private $con; 

  function __construct(){

   require_once dirname(__FILE__).'/DbConnect.php';

   $db = new DbConnect();

   $this->con = $db->connect();

  }

  /*CRUD -> C -> CREATE */

  function createUser($username, $pass, $email){
    $password = md5($pass);
    $stmt = $this->con->prepare("INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES (NULL,?,?,?);");
    $stmt->bind_param("sss",$username,$password,$email);

    if($stmt->execute()){
     return true; 
    }else{
     return false; 
    }
   }
  }

<?php 

require_once '../includes/DbOperations.php';

$response = array(); 

if($_SERVER['REQUEST_METHOD']=='POST'){

 if(
  isset($_POST['username']) and 
   isset($_POST['password']) and 
    isset($_POST['email']))
  {
   //operator the data further
   $db = new DbOperations();

   if($db->createUser(
    $_POST['username'],
    $_POST['password'],
    $_POST["email"]
   )){
    $response['error'] = false;
    $response['message'] = "User registered successfully";
   }else{
    $response['error']= true;
    $response['message']= "Error Creating User";
   }

  }else{
   $response['error'] = true;
   $response['message'] = "Required Fields are missing";
  }

}else{
 $response['error'] = true;
 $response['message'] ="Invalid Request";
}
 
echo json_encode($response);
moein
  • 1
  • 1
  • It would help with your own coding if you get some useful errors when something fails - https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query – Nigel Ren Mar 21 '20 at 11:35
  • **Warning!** Don't use md5 for password hashing! [The manual](https://www.php.net/manual/en/function.md5.php) even states: _"Warning - It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm"_. You should use PHP's [password_hash()](https://www.php.net/manual/en/function.password-hash.php) to create a secure hash. Then you can use [password_verify()](https://www.php.net/manual/en/function.password-verify.php) to verify a password against a hash. – M. Eriksson Mar 21 '20 at 11:39
  • i used " or die(mysqli_error($db)); " in the end of the mysql code for showing errors but there was no error to show. – moein Mar 21 '20 at 11:43

0 Answers0