gpt4 book ai didi

php - 无法为对象输入值。导致 JSON 对象响应

转载 作者:行者123 更新时间:2023-11-29 02:41:20 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于php - 无法为对象输入值。导致 JSON 对象响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com