gpt4 book ai didi

php - 在 PHP 中返回 JSON 消息

转载 作者:行者123 更新时间:2023-11-28 23:15:52 25 4
gpt4 key购买 nike

我编写了一个 PHP 脚本来连接到 MySQL 数据库并提取一些信息。我知道大部分脚本都在工作,因为它实际上将 MAC 地址插入表中(当 DB 表中的 MAC 地址值为 NULL 时,它应该这样做)。

但是,我的脚本生成的消息是“未找到许可证 (3)”。

我的问题是,如果它插入 MAC 地址,它如何返回此消息?只有当 if (mysqli_num_rows($result) > 0) 返回 false 时,才会输入 else 语句。

我想要的是检查 MAC 地址(如果数据库中为 NULL,则插入)并返回消息“已许可”。

为嵌套的 if/else 语句道歉。

<?php

// Array for JSON response.
$response = array();

require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();

// Check for GET data.
if (isset($_GET["LicenseKey"])
&& isset($_GET["SoftwareId"])
&& isset($_GET["MacAddress"])) {

$licenseKey = $_GET['LicenseKey'];
$softwareId = $_GET['SoftwareId'];
$macAddress = $_GET['MacAddress'];

// Import database connection variables.
require_once __DIR__ . '/get_license_SQL.php';

$query = SQL_GET_LICENSE;
$query = str_replace("%1", $softwareId, $query);
$query = str_replace("%2", $licenseKey, $query);

$result = mysqli_query($db->connect(), $query);

if (!empty($result)) {
// Check for empty result.
if (mysqli_num_rows($result) > 0) {
// Get the result.
$result = mysqli_fetch_array($result);
$license = array();
$license["ExpiryDate"] = $result["ExpiryDate"];

// Check if MAC address exists.
$query = SQL_GET_MAC_ADDRESS;
$query = str_replace("%1", $licenseKey, $query);
$result = mysqli_query($db->connect(), $query);

if (!empty($result)) {
$result = mysqli_fetch_array($result);
echo json_encode($result);

if ($result["MacAddress"] == $macAddress
|| $result["MacAddress"] == NULL) {

// Device MAC address matches MAC address on record.
$response["success"] = 1;
$response["license"] = array();

if ($result["MacAddress"] == NULL) {
// Insert new MAC address into the database.
$query = SQL_INSERT_MAC_ADDRESS;
$query = str_replace("%1", $macAddress, $query);
$query = str_replace("%2", $licenseKey, $query);
mysqli_query($db->connect(), $query);
}

// Add MAC address to license array.
$license["MacAddress"] = $result["MacAddress"];
$response["message"] = "Licensed";
array_push($response["license"], $license);
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "License has already been used by another device";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "No license found (2)";
array_push($response["license"], $license);
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "No license found (3)";
array_push($response["license"], $license);
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "No license found (4)";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) missing";
echo json_encode($response);
}
?>

数据库连接.php :

<?php

class DB_CONNECT {

function __construct() {
$this->connect();
}

function connect() {
require_once __DIR__ . '/db_config.php';
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);
$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
return $con;
}
}
?>

get_license_SQL.php :

<?php
define('SQL_GET_LICENSE', "SELECT licenses.ExpiryDate
FROM licenses
WHERE licenses.SoftwareId=%1 AND licenses.LicenseKey=%2");

define('SQL_GET_MAC_ADDRESS', "SELECT licenses.MacAddress
FROM licenses
WHERE licenses.LicenseKey=%1");

define('SQL_INSERT_MAC_ADDRESS', "UPDATE licenses
SET MacAddress=%1
WHERE licenses.LicenseKey=%2");
?>

最佳答案

请看下面几行代码

                if ($result["MacAddress"] == NULL) {    
// Insert new MAC address into the database.
$query = SQL_INSERT_MAC_ADDRESS;
$query = str_replace("%1", $macAddress, $query);
$query = str_replace("%2", $licenseKey, $query);
mysqli_query($db->connect(), $query);
}

// Add MAC address to license array.
$license["MacAddress"] = $result["MacAddress"];

您正在运行以下查询,但在插入数据库后没有将结果存储在变量中: mysqli_query($db->connect(), $query);

在下一行中,$result 数组包含在插入之前从数据库中获取的旧值。请尝试将值存储在变量中并在插入后使用该变量。 $license["MacAddress"] = $result["MacAddress"];

关于php - 在 PHP 中返回 JSON 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43916286/

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