-3

I have been facing this problem every now and then. I have tried many ways but have not got any success. How can I figure out where I am going wrong? This is my PHP script:

    <?php 
    if (isset($_SERVER['HTTP_ORIGIN'])) {
            header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");

            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');    // cache for 1 day
        }

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

            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

            exit(0);
        }


    $mysql_host = "localhost";

    $mysql_database = "locationtracker";

    $mysql_user = "root";

    $mysql_password = "";

    // Create connection

    $conn = new mysqli($mysql_host, $mysql_user, $mysql_password,$mysql_database);





    if ($conn->connect_error) {

        //die("Connection failed: " . $conn->connect_error);

    } 



    $postdata = file_get_contents("php://input");

        if (isset($postdata)) {

            $request = json_decode($postdata);

             $user= $request->username;



                    }






        $sql = "SELECT u_id FROM user_info WHERE login_id = '$user'";

          $result = mysqli_query($conn,$sql);

          $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

          $active = $row['active'];

          $count = mysqli_num_rows($result);




          if($count >0) {

         $response= "Your Login success";

          }else {

        $response= "Your Login Email or Password is invalid";         

          }

     echo json_encode( $response);

    ?>

Note: This script will be used for ionic login authentication part.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Tobias F. Sep 21 '18 at 07:13
  • 1
    Which line php complain? I see two variants: 1. Connection not established (username/pass possibly wrong) 2. Returned data of json_decode, does not contain username. Your problem is too simple. – Velaro Sep 21 '18 at 07:24
  • 1
    You also try to access $row['active'], but in the MySQL query you do not select active, try SELECT `u_id`, `active` FROM user_info WHERE login_id = '$user' – Rolfie Sep 21 '18 at 07:39
  • @Velaro error is on this line $user= $request->username; – shashank verma Sep 21 '18 at 10:07
  • There would be another error such as "Undefined index" @Rolfie – Velaro Sep 21 '18 at 11:49
  • @shashankverma, please provide us contents of $postdata and json_decode`s response. Json_decode may return false, or $postdata may not contain username. So you must check with isset/property_exist functions – Velaro Sep 21 '18 at 11:53
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – faintsignal Sep 25 '18 at 03:10

1 Answers1

0

You are getting an error because of $row['active'] you do not select column active in a select statement. You change a little bit code of your SQL query and if condition and it works. Please try this is help you.

 $sql = "SELECT u_id FROM user_info WHERE login_id = '$user'";

  $result = mysqli_query($conn,$sql);

  if(mysqli_num_rows($result) >0) 
   {
      $response= "Your Login success";
   } 
   else 
   {
      $response= "Your Login Email or Password is invalid";         
   }
Subhash Patel
  • 674
  • 7
  • 16
  • Hi @shashankverma please change $request = json_decode($postdata,true); in your code and also change $user= $request['username']; try and check. – Subhash Patel Sep 21 '18 at 10:16
  • hi. I tried your suggestion it removes the error but i get this in the response "Your Login Email or Password is invalid" even when the data is present in the table. :( – shashank verma Sep 21 '18 at 10:33
  • Please echo your select query and check in the database user_info table directly and also echo $user= $request['username], and check you received post data or not. – Subhash Patel Sep 21 '18 at 11:52