2015-03-31 109 views
0

我想从android端接收json数据。 Android端发送用户名和密码在json到这个PHP端。 这是我的PHP代码:http://localhost/myproject/index.php/user/getuserlogin比较json数据接收从android到数据库数据在php

的错误是::

public function actionGetUserLogin() 
    { 
     // array for JSON response 
     $response = array(); 


     $conn=mysqli_connect("localhost","root","","db"); 
     $user_login = "select * from user" ; 
        $query = mysqli_query ($conn, $user_login); 

        while($results = mysqli_fetch_array ($query)){ 
          $user_name = $results['mobile_user_name']; 
          $pass = $results['mobile_user_pass']; 
          echo $user_name; 
          echo "</br>"; 
          echo $pass; 
          echo "</br>"; 
        } 

     //compare the POST data from user with the existing username and  password in database table 


if($_POST['username']==$user_name&&$_POST['username']!=""&&$_POST['password']==$pass&&$_POST['password']!="") 
     { 
      //if match with database record 
      $response["success"] = 1; 
      $response["message"] = "correct"; 
     }else{ 
      $response["success"] = 0; 
      $response["message"] = "wrong"; 
     } 

     echo json_encode($response); 
     Yii::app()->end(); 
    } 

当我使用这个网址测试这个方法,我得到一个错误未定义指数:用户名

是它接受json时,我的php出错了? 有没有办法显示android发送的json数据?

+0

你必须确保表单使用post方法。 – 4EACH 2015-03-31 22:54:09

回答

0
public function actionGetUserLogin() 
    { 

创建数据库连接

if(!isset($_POST['username'],$_POST['password']) || strlen($_POST['password'])*strlen($_POST['username'])==0){ 
header($_SERVER['SERVER_PROTOCOL']. ' 401 Unauthorized'); 
return false; 
} 


     // array for JSON response 
     $response = array(); 

之前加入这个检查,我建议你实现数据源连接的单

  $conn=mysqli_connect("localhost","root","","db"); 

  $user_login = "select * from user" ; 
        $query = mysqli_query ($conn, $user_login); 

        while($results = mysqli_fetch_array ($query)){ 
          $user_name = $results['mobile_user_name']; 
          $pass = $results['mobile_user_pass']; 
          echo $user_name; 
          echo "</br>"; 
          echo $pass; 
          echo "</br>"; 
        } 

     //compare the POST data from user with the existing username and  password in database table 

if($_POST['username']==$user_name&&$_POST['username']!=""&&$_POST['password']==$pass&&$_POST['password']!="") 
      { 
       //if match with database record 
       $response["success"] = 1; 
       $response["message"] = "correct"; 
      }else{ 
       $response["success"] = 0; 
       $response["message"] = "wrong"; 
      } 

      echo json_encode($response); 
      Yii::app()->end(); 
     }