php - 为什么这个 SQL 没有插入到数据库中?

标签 php mysql json pdo

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>

当在我的网络浏览器中访问脚本 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 列,它们是返回用户表的外键。如果您定期查询此列,它比非结构化文本列更有意义。

关于php - 为什么这个 SQL 没有插入到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38548285/

相关文章:

php - 如何在 php 中刷新数据并断开用户连接但保持脚本事件

PHP/MySQL : Dynamic prepared statement with insert/update query

php - 重新排列 PHP 页面,使 header 位于 HTML 之前

mysql - Jenkins 显示 "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

javascript - 将 JSON 数组从 Controller 传递到 JavaScript 中的变量

java - 如何将 JSON 请求解析为 FlightOfferSearch[] 对象?

php - 如何让这个 PHP/MySQL 游戏 map 更加高效?

php - CodeIgniter 负载 Controller View

mysql - Rails 4 - 按相关模型排序 created_at - 事件

c# - 如何将两个 float 组的 JSON 解析为两个列表?