dongzhugao9929 2016-07-23 18:53 采纳率: 0%
浏览 15
已采纳

为什么这个SQL没有插入数据库?

PHP Version: 7.0

Script is sent data from a different website.

For some reason, the data is not being inserted into the database like it should be, and I don't think I have any SQL errors (this is done with PDO).

Here is the included functions code:

<?php
function escape($string){
    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
?>

Script Code:

    <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>

When go to the script URL in my web browser, the HTML shows up, and no errors.

展开全部

  • 写回答

1条回答 默认 最新

  • doushan5245 2016-08-02 16:30
    关注

    Your problem is almost certainly with the request coming in, but here are a few issues you could address with your code.

    • htmlspecialchars() is not for inserting into a database. It's used when you want to display something as HTML.
    • none of those values you're checking will ever be null, because you're running them through htmlspecialchars() which returns a string.
    • there's no need to use PDOStatement::bindParam() unless you need to do something special with data types. Just pass an array to PDOStatement::execute() instead.
    • it sounds like you're not recording any error messages. If you aren't using this page interactively, you need to have some way to know if there's a problem.

    With that in mind, I'd recommend trying this:

    <?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");
        }
    }
    ?>
    

    You'll want to make sure that you create the log file ahead of time, with the correct permissions for your web server to be able to write to it. On a Linux platform this might look like sudo touch /var/log/php && sudo chown www-data /var/log/php

    Also I'm assuming you're using a current version of PHP that supports the null coalesce operator; you'll need to replace $foo = $bar ?? null with $foo = isset($bar) ? $bar : null if that's not the case.

    One more point, if each user on your system has an entry in a user table, you should really have UserID and SenderID columns in the PlayerBans table that are foreign keys back to your users table. If you're querying this column regularly it makes a whole lot more sense than having an unstructured text column.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部