首页 > 解决方案 > 无法为对象输入值。导致 JSON 对象响应

问题描述

我有一个工作 php 代码,它从我的 Android 应用程序的 JsonObjectRequest 获取 id 和 deviceID。问题是什么时候在“function authenticateUser”中。我似乎无法执行此操作 $response->auth = "1"; .

当我评论此代码时,它不会产生任何错误并为我提供正确的输出,但对象身份验证为空。我可以在对象( response->$isAuthenticatedresponse->$isSameUser )中输入这两个变量的值,但是当我尝试输入(response->$auth)时会产生错误

class Response{
    public $isAuthenticated;
    public $isSameUser;
    public $auth;
}

$response = new Response();
$error = array();
$log= array();

if(isset($decoded['id']) && isset($decoded['deviceID'])){
    $conn = mysqli_connect($servername,$username,$password,$dbname);
    $id = $decoded['id'];
    $deviceID = $decoded['deviceID'];


    if (mysqli_connect_errno())
    {
        array_push($error,"Failed to connect to MySQL: " . mysqli_connect_error());
    }
    else
    {
        $response -> isAuthenticated = checkIfAlreadyAuthenticated($conn, $id);
        if($response -> isAuthenticated ==0){
            array_push($log, $response -> isAuthenticated);
            authenticateUser($response, $conn, $id, $deviceID);

        }
        elseif($response -> isAuthenticated ==1){
            array_push($log, $response -> isAuthenticated);
            $response -> isSameUser = checkIfSameUser($conn, $id, $deviceID);
        }

    }

}
else{
    //echo 'POST ERROR';
}

function checkIfSameUser($conn, $id, $deviceID){
    $sql = "SELECT pin, deviceID FROM nextrack_userauthentication";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $id_fromDB = $row["pin"]; 
            $deviceID_fromDB = $row["deviceID"];
        }
    } else {
        //echo "checkifSameUser Method SQL ERROR";
    }

    if((($id_fromDB == $id) == TRUE) AND (($deviceID_fromDB == $deviceID)== TRUE)){
        return 1;
    }

}

function authenticateUser($conn, $id, $deviceID){
    $authenticate = "1";
    $sql = "INSERT INTO nextrack_userauthentication(pin, activated, deviceID) VALUES ('".$id."','".$authenticate."','".$deviceID."')";
    mysqli_query($conn, $sql);

    if(mysqli_affected_rows($conn)>0)
    {
        $response->auth = "1";
    } 
    else 
    {
        $response->auth = "0";
    }

}

function checkIfAlreadyAuthenticated($conn, $id){
    $sql = "SELECT EXISTS(SELECT'". $id ."' FROM nextrack_userauthentication WHERE pin='" . $id ."'";

    $result = $conn->query("SELECT '". $id ."' FROM nextrack_userauthentication WHERE pin='" . $id ."'");

    if($result->num_rows == 0) {
        return 0;
    } else {
        return 1;
    }
}

echo json_encode($response, JSON_FORCE_OBJECT);

标签: phpandroidmysqlmysqli

解决方案


您最好authenticateUser()只返回 1 或 0,这取决于登录是否成功,并在调用部分分配它。这意味着它authenticateUser()不直接链接到响应,而只是操作是否正常的一种情况......

   function authenticateUser($conn, $id, $deviceID){
        $authenticate = "1";
        $sql = "INSERT INTO nextrack_userauthentication(pin, activated, deviceID) VALUES ('".$id."','".$authenticate."','".$deviceID."')";

        mysqli_query($conn, $sql);

        if(mysqli_affected_rows($conn)>0)
        {
            return "1";
        } 
        else 
        {
            return "0";
        }

    }

接着...

    if($response -> isAuthenticated ==0){
        $response->auth = authenticateUser($conn, $id, $deviceID);
    }    

您也可以使用authas true 或 false ,然后您的功能变为...

   function authenticateUser($conn, $id, $deviceID){
        $authenticate = "1";
        $sql = "INSERT INTO nextrack_userauthentication(pin, activated, deviceID) VALUES ('".$id."','".$authenticate."','".$deviceID."')";

        mysqli_query($conn, $sql);

        return (mysqli_affected_rows($conn)>0);
    }

推荐阅读