我正在更新数据库中的 2 个表。一个有效 HTTPURLResponse 200。另一个失败响应 503。
不快速编码的人。所有参数值均已设置且有效。显示我的 POST 方法,但 GET 也失败并出现 503 错误。
private func post() async throws {
let rcid = UserDefaults.standard.value(forKey: DefaultKey.rcID.rawValue) as? String ?? "NOT SET"
guard let url = URL(string: "https://example.com/folder/subfolder/insertUserIntoDBPOST.php") else {
throw URLError(.badURL)
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
let parameters = [
URLQueryItem(name: "Line", value: selectedLine),
URLQueryItem(name: "Depot", value: selectedDepot),
URLQueryItem(name: "RCID", value: rcid),
URLQueryItem(name: "RemainingFreeLaunches", value: String(remainingFreeLaunches)),
URLQueryItem(name: "SubscriptionStatus", value: subscriptionDuration.rawValue)
]
let parameterString = parameters.map { "\($0.name)=\($0.value ?? "")" }.joined(separator: "&")
request.httpBody = parameterString.data(using: .utf8)
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw URLError(.badServerResponse)
}
if httpResponse.statusCode == 200 {
// do something with a successful update
} else {
/// throw the error (for eg 503)
throw URLError(.badServerResponse)
}
} catch {
throw error
}
}
服务器端PHP
<?php
require_once("../../includes/connection_auth.php");
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
if (isset($_POST['Line'], $_POST['Depot'], $_POST['RCID'], $_POST['RemainingFreeLaunches'], $_POST['SubscriptionStatus'])) {
$line = mysqli_real_escape_string($connection, $_POST['Line']);
$depot = mysqli_real_escape_string($connection, $_POST['Depot']);
$rcid = mysqli_real_escape_string($connection, $_POST['RCID']);
$remainingFreeLaunches = (int) $_POST['RemainingFreeLaunches'];
$subscriptionStatus = mysqli_real_escape_string($connection, $_POST['SubscriptionStatus']);
} else {
die("Missing POST data. Please ensure all required fields are provided.");
}
$insertQuery = "INSERT INTO usersTable (Line, Depot, RCID, RemainingFreeLaunches, SubscriptionStatus)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
Line = VALUES(Line), Depot = VALUES(Depot), RemainingFreeLaunches = VALUES(RemainingFreeLaunches), SubscriptionStatus = VALUES(SubscriptionStatus);";
$stmt = mysqli_prepare($connection, $insertQuery);
if ($stmt === false) {
die("Error preparing query: " . mysqli_error($connection));
}
mysqli_stmt_bind_param($stmt, "sssis", $line, $depot, $rcid,$remainingFreeLaunches, $subscriptionStatus);
if (mysqli_stmt_execute($stmt)) {
echo "Record inserted/updated successfully";
} else {
echo "Error: " . mysqli_stmt_error($stmt);
}
mysqli_stmt_close($stmt);
$connection->close();
?>
已解决:该问题是由于语法与 MariaDB 不兼容所致。
改变了
$insertQuery = "INSERT INTO usersTable (Line, Depot, RCID, RemainingFreeLaunches, SubscriptionStatus)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
Line = VALUES(Line), Depot = VALUES(Depot), RemainingFreeLaunches = VALUES(RemainingFreeLaunches), SubscriptionStatus = VALUES(SubscriptionStatus);";
到
$insertQuery = "INSERT INTO usersTable (Line, Depot, RCID, RemainingFreeLaunches, SubscriptionStatus)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
Line = ?, Depot = ?, RemainingFreeLaunches = ?, SubscriptionStatus = ?;";