1

I am trying fetch data from Database and check if the returned result is empty or not. This is my code.

<?php
require "init1.php";

$jsonObject = $_GET["UserDetails"];
$obj = json_decode($jsonObject);

$email = $obj->Email;
$password = $obj->Password;
$Username = $obj->Username;
$Sex = $obj->Sex;

$timestamp = strtotime($obj->BirthDay);
$BirthDay = date('Y-m-d', $timestamp);

$sql = "select * from Registered_Users where Username='$Username' or Email='$Email'";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {

    $myclass = new stdClass();
    $myclass->status = "Not Ok";

    echo json_encode($myclass);

} else {

    try {
        $sql_query = "insert into Registered_Users (Email, Password, BirthDay, Sex, Username) values('$email', '$password', '$BirthDay', '$Sex', '$Username')";
        if (mysqli_query($con, $sql_query)) {
            $obj1 = new stdClass();
            $obj1->status = "Ok";
            echo json_encode($obj1);
        } else {
            $obj1 = new stdClass();
            $obj1->status = "Not Ok";
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
?>    

Can i know whats wrong with my above code, What's happening is i am checking if the username or email is present in the Database if so, It will print not ok else it will print ok . But when i run the code even if the $result is greater than it's echoing ok, when i run the same code second time it says NOT OK. Can some one tell me what's wrong .

Thank You

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42

1 Answers1

2

Change the variable name $Email and use $email

$sql = "select * from Registered_Users where Username='$Username' or Email='$email'";
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75