【问题标题】:Compare json data receive from android to database data in php比较从android接收到的json数据和php中的数据库数据
【发布时间】:2015-03-31 22:39:15
【问题描述】:

我想从 android 端接收 json 数据。 android端正在将json中的用户名和密码发送到这个php端。 这是我的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();
    }

当我使用此 url 测试此方法时出现错误:http://localhost/myproject/index.php/user/getuserlogin

错误是:未定义索引:用户名

在接受 json 时我的 php 出现问题了吗? 有没有办法显示android发送的json数据?

【问题讨论】:

  • 您必须确保该表单使用 post 方法。

标签: php json


【解决方案1】:
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();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多