不能输入对象的值。导致JSON对象响应

时间:2018-07-14 14:47:21

标签: php android mysql mysqli

我有一个有效的php代码,该代码从我的Android应用程序的JsonObjectRequest中获取ID和deviceID。问题是何时在“函数authenticateUser”中。我似乎无法执行此操作$ response-> auth =“ 1”;

当我注释此代码时,它不会产生任何错误,并且会为我提供正确的输出,除非我获得对象身份验证的null。我可以在对象( response-> $ isAuthenticated response-> $ 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);

1 个答案:

答案 0 :(得分:1)

取决于登录成功与否,最好使authenticateUser()仅返回1或0,并在调用部分中分配此值。这意味着authenticateUser()并没有直接链接到响应,只是操作OK或不行的一种情况。

   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);
    }    

您还可以将auth用作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);
    }