PHP版本:7.0
脚本是从其他网站发送数据的。
由于某种原因,数据没有像应有的那样插入数据库,而且我认为我没有任何SQL错误(这是通过PDO完成的)。
这是包含的功能代码:
<?php
function escape($string){
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
?>
脚本代码:
<html>
<head>
<title>Data from Roblox</title>
<h3>Data from Roblox</h3>
</head>
<body>
<?php
include '../includes/connection.php';
include '../scripts/functions.php'; //Remove if unknown error as well as the escapes
error_reporting(E_ALL);
ini_set('display_errors', 1);
$array = json_decode(file_get_contents('php://input'),1);
$SenderName = escape($array['SenderName']);
$SenderID = escape($array['SenderID']);
$PlayerName = escape($array['PlayerName']);
$PlayerID = escape($array['PlayerID']);
$Reason = escape($array['Reason']);
$PlaceLink = escape($array['PlaceLink']);
if(!$Reason){ $Reason = "Reason not provided."; }
if($SenderName !=NULL and $SenderID != NULL and $PlayerName != NULL and $PlayerID !=NULL and $PlaceLink !=NULL){
$query = $handler->prepare("INSERT INTO PlayerBans (`ID`, `Username`,`Reason`, `BannedDate`, `BannedBy`, `BannedAt`) VALUES (:pid, :pname, :reason, NOW(), :sname, :pl)");
$query->bindParam(':pid', $PlayerID);
$query->bindParam(':pname', $PlayerName);
$query->bindParam(':reason', $Reason);
$sender = $SenderName . " - " . $SenderID;
$query->bindParam(':sname', $sender);
$query->bindParam(':pl', $PlaceLink);
$query->execute();
}
?>
</body>
</html>
在我的Web浏览器中转到脚本URL时,将显示HTML,并且没有错误。
最佳答案
几乎可以肯定,您的问题来自请求的传入,但是这里有一些您可以使用代码解决的问题。htmlspecialchars()
不适用于插入数据库。当您想要将某些内容显示为HTML时使用。
您正在检查的所有值都不会为null,因为您正在通过htmlspecialchars()
运行它们,该返回字符串。
除非您需要对数据类型做一些特殊的事情,否则无需使用PDOStatement::bindParam()
。只需将数组传递给PDOStatement::execute()
即可。
听起来您没有录制任何错误消息。如果您不是交互式使用此页面,则需要某种方式来了解是否存在问题。
考虑到这一点,我建议您尝试以下操作:
<?php
include("../includes/connection.php");
error_reporting(E_ALL);
ini_set("display_errors", true);
ini_set("error_log", "/var/log/php.log");
$json = file_get_contents("php://input");
$array = json_decode($json, true);
$SenderName = $array['SenderName'] ?? null;
$SenderID = $array['SenderID'] ?? null;
$PlayerName = $array['PlayerName'] ?? null;
$PlayerID = $array['PlayerID'] ?? null;
$Reason = $array['Reason'] ?? "Reason not provided";
$PlaceLink = $array['PlaceLink'] ?? null;
if($SenderName !== null && $SenderID !== null && $PlayerName !== null && $PlayerID !== null && $PlaceLink !== null) {
// prepare using ? for a shorter query; don't mix placeholders with other values
$query = $handler->prepare("INSERT INTO PlayerBans (`ID`, `Username`,`Reason`, `BannedBy`, `BannedAt`, `BannedDate`) VALUES (?,?,?,?,?,NOW())");
// double quotes interpolate variables!
$sender = "$SenderName - $SenderID";
// pass the values directly to execute
$result = $query->execute([$PlayerID, $PlayerName, $Reason, $sender, $PlaceLink]);
// check the result of this call and log some details if there's a problem
if (!$result) {
$e = $query->errorInfo();
error_log("SQL Error $e[0]: $e[2] ($e[1]) while inserting data: $json");
}
}
?>
您需要确保您提前创建了日志文件,并为您的Web服务器写入了正确的权限。在Linux平台上,它可能看起来像
sudo touch /var/log/php && sudo chown www-data /var/log/php
另外,我假设您正在使用支持null coalesce operator的最新版本的PHP。如果不是这种情况,则需要将
$foo = $bar ?? null
替换为$foo = isset($bar) ? $bar : null
。还有一点,如果系统上的每个用户在用户表中都有一个条目,则在PlayerBans表中确实应该有UserID和SenderID列,这些列是返回到users表的外键。如果您要定期查询此列,则比使用非结构化文本列要有意义得多。
关于php - 为什么此SQL不插入数据库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38548285/