1

i am new to this and i am stuck with this error

Undefined property: stdClass::$id in C:\xampp\htdocs\thesisApp\db\login.php on line 6


here is the code

<?php 
$con = new mysqli("localhost", "root", "root", "thesisAppdb");
$data = json_decode(file_get_contents("php://input"));
$id = mysqli_real_escape_string($con, $data->id);
$username = mysqli_real_escape_string($con, $data->username);
$password = mysqli_real_escape_string($con, $data->password);

 $query = ("SELECT id FROM teacherdata WHERE username= '$username' and 
 password= '$password' and id = '$id'");
 $que = mysqli_query($con, $query);
 $count = mysqli_num_rows($que);

 if ($count == 1) {
 echo 'correct';

} else {
echo 'wrong';
}
?>

hope to hear news from anyone.

7
  • Your $id is still an object? can you print $id for debugging Commented Oct 26, 2017 at 10:28
  • Please, provide the output of var_dump($data). Commented Oct 26, 2017 at 10:29
  • @mega6382 object(stdClass)#2 that is the output Commented Oct 26, 2017 at 10:30
  • OK, now can you provide and check the output of var_dump(file_get_contents("php://input")) Commented Oct 26, 2017 at 10:31
  • <b>Parse error</b>: syntax error, unexpected '$query' (T_VARIABLE) @mega6382 this what it outputed Commented Oct 26, 2017 at 10:36

1 Answer 1

2

As you can see from the output of var_dump, that file_get_contents("php://input") contains an empty json object. Which when decoded provides an empty PHP object. So, you need to make sure that there is id key in the json object and on the PHP side you can use the following:

$data = json_decode(file_get_contents("php://input"));
$id = 0;
if(isset($data->id))
{
    $id = mysqli_real_escape_string($con, $data->id);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.