无法输入对象的值导致json对象响应

omjgkv6w  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(363)

我有一个可以工作的php代码,它从我的android应用程序的jsonobjectrequest获取id和deviceid。问题是什么时候在“函数authenticateuser”中。我似乎不能做这个操作$response->auth=“1”;。
当我注解这段代码时,它不会产生任何错误,并给出正确的输出,除非我为objectauth获取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);
pbpqsu0x

pbpqsu0x1#

你最好还是出去走走 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);
    }

你也可以用 auth 不管是真是假然后你的函数就变成。。。

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

相关问题