我想从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();
}
当我使用此网址测试此方法时出现错误:http://localhost/myproject/index.php/user/getuserlogin
错误是:未定义索引:用户名
接受json时我的php出了什么问题? 有没有办法显示android发送的json数据?
答案 0 :(得分: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();
}