-1

This is the php code

<?php
   $con=mysqli_connect("localhost","root","","my_db");

   $response = array();
    header('Content-Type: application/json');


   if (mysqli_connect_errno($con)) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

   $username = $_POST['_username'];
   $password = $_POST['_password'];

   var_dump($_POST);
   $result = mysqli_query($con,"SELECT Role FROM user_details where 
   useremail='$username' and userpassword='$password'");
   $row = mysqli_fetch_array($result);
   $data = $row[0];

   if($data){
      echo $data;
   }

   mysqli_close($con);
?>

http://localhost/android_login_api/mycon.php?username=m@gmail.com&password=1234

It gives the following error

Undefined index: username in C:\xampp\htdocs\android_login_api\mycon.php on line 12

I tried the GET, without passing parameters then I can return the values. But here I am working on a login screen so I need to use POST method to capture the values.

Appreciate your guidance on this Using xampp V3.2.4

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Apr 07 '20 at 09:26

1 Answers1

2

You are accessing post data by keys _username and _password

$username = $_POST['_username'];
$password = $_POST['_password'];

Whereas in postman you are supplying http://localhost/android_login_api/mycon.php?username=m@gmail.com&password=1234 ie username and password

That is why you are getting undefined index error at line no 12 ie $username = $_POST['_username'];

Either change to

$username = $_POST['username'];
$password = $_POST['password']; 

Or change call as

http://localhost/android_login_api/mycon.php?_username=m@gmail.com&_password=1234

UPDATE:

Actually you are doing it the wrong way, API in PHP is not created the way you are doing. Here is a good tutorial

Modify your code as follows, I have removed other parts

<?php
$data = json_decode(file_get_contents("php://input"));
echo('Name: ' . $data->username .'<br />');
echo('Password: ' . $data->password);
?>

and from postman

enter image description here

M.D
  • 270
  • 2
  • 11