2017-10-15 76 views
0
<?php 

    require_once 'DbConnect.php'; 

    //an array to display response 
    $response = array(); 

    //if it is an api call 
    //that means a get parameter named api call is set in the URL 
    //and with this parameter we are concluding that it is an api call 

    if(isset($_GET['apicall'])){ 

     switch($_GET['apicall']){ 

      case 'signup': 
       //checking the parameters required are available or not 
       if(isTheseParametersAvailable(array('username','email','password','gender'))){ 

        //getting the values 
        $username = $_POST['username']; 
        $email = $_POST['email']; 
        $password = md5($_POST['password']); 
        $gender = $_POST['gender']; 

        //checking if the user is already exist with this username or email 
        //as the email and username should be unique for every user 
        $stmt = $conn->prepare("SELECT id FROM users WHERE username = ? OR email = ?"); 
        $stmt->bind_param("ss", $username, $email); 
        $stmt->execute(); 
        $stmt->store_result(); 

        //if the user already exist in the database 
        if($stmt->num_rows > 0){ 
         $response['error'] = true; 
         $response['message'] = 'User already registered'; 
         $stmt->close(); 
        }else{ 

         //if user is new creating an insert query 
         $stmt = $conn->prepare("INSERT INTO users (username, email, password, gender) VALUES (?, ?, ?, ?)"); 
         $stmt->bind_param("ssss", $username, $email, $password, $gender); 

         //if the user is successfully added to the database 
         if($stmt->execute()){ 

          //fetching the user back 
          $stmt = $conn->prepare("SELECT id, id, username, email, gender FROM users WHERE username = ?"); 
          $stmt->bind_param("s",$username); 
          $stmt->execute(); 
          $stmt->bind_result($userid, $id, $username, $email, $gender); 
          $stmt->fetch(); 

          $user = array(
           'id'=>$id, 
           'username'=>$username, 
           'email'=>$email, 
           'gender'=>$gender 
          ); 

          $stmt->close(); 

          //adding the user data in response 
          $response['error'] = false; 
          $response['message'] = 'User registered successfully'; 
          $response['user'] = $user; 
         } 
        } 

       }else{ 
        $response['error'] = true; 
        $response['message'] = 'required parameters are not available'; 
       } 

      break; 

      case 'login': 
       //for login we need the username and password 
       if(isTheseParametersAvailable(array('username', 'password'))){ 
        //getting values 
        $username = $_POST['username']; 
        $password = md5($_POST['password']); 

        //creating the query 
        $stmt = $conn->prepare("SELECT id, username, email, gender FROM users WHERE username = ? AND password = ?"); 
        $stmt->bind_param("ss",$username, $password); 

        $stmt->execute(); 

        $stmt->store_result(); 

        //if the user exist with given credentials 
        if($stmt->num_rows > 0){ 

         $stmt->bind_result($id, $username, $email, $gender); 
         $stmt->fetch(); 

         $user = array(
          'id'=>$id, 
          'username'=>$username, 
          'email'=>$email, 
          'gender'=>$gender 
         ); 

         $response['error'] = false; 
         $response['message'] = 'Login successfull'; 
         $response['user'] = $user; 
        }else{ 
         //if the user not found 
         $response['error'] = false; 
         $response['message'] = 'Invalid username or password'; 
        } 
       } 
      break; 

      default: 
       $response['error'] = true; 
       $response['message'] = 'Invalid Operation Called'; 
     } 

    }else{ 
     //if it is not api call 
     //pushing appropriate values to response array 
     $response['error'] = true; 
     $response['message'] = 'Invalid API Call'; 
    } 

    //displaying the response in json structure 
    echo json_encode($response); 

    //function validating all the paramters are available 
    //we will pass the required parameters to this function 
    function isTheseParametersAvailable($params){ 

     //traversing through all the parameters 
     foreach($params as $param){ 
      //if the paramter is not available 
      if(!isset($_POST[$param])){ 
       //return false 
       return false; 
      } 
     } 
     //return true if every param is available 
     return true; 
    } 

enter image description here[]在郵遞員響應PHP

我不斷收到[]在postman.What我期待的效應初探是

https://i2.wp.com/www.simplifiedcoding.net/wp-content/uploads/2015/08/user-registration.png?w=954&ssl=1

請幫助我。我想在我的Android應用程序中使用PHP實現登錄功能,因爲我不知道PHP,我不知道什麼是錯的。請幫助

回答

0

你必須只創建一個登錄細節,如對象:USER_EMAIL,USER_ID,user_gender,USER_NAME等你有沒有需要你的JSON編碼中的數組,所以只是傳遞對象

EG

{ 
    status=true/false, 
    message="your message", 
    user_id="[email protected]", 
    ,etc(Other objects) 
} 

你必須根據其ID是拿到用戶數據,並且只要做到以上:

$myObj->name = "John"; //fetch user name 
$myObj->age = 30;  //fetch user age 
$myObj->city = "New York"; //fetch user city 

$myJSON = json_encode($myObj); // edcode JSON 
+0

你能告訴我在哪裏需要進行更改以及需要做什麼確切的更改,因爲我對php沒有任何瞭解。請 – hagrya

+0

我對PHP沒有很好的瞭解,但我認爲你必須刪除創建數組$ user = array( 'id'=> $ id, 'username'=> $ username, 'email'=> $ email, 'gender'=> $ gender ); –

+0

我不認爲這是錯誤,因爲我只是從簡化編碼複製代碼 – hagrya