2016-07-24 148 views
0

PHP版本:7.0爲什麼這個SQL沒有插入到數據庫中?

腳本從不同的網站發送數據。

出於某種原因,數據沒有被插入到數據庫中,因爲它應該是,我不認爲我有任何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顯示出來,並沒有錯誤。

+0

不相關...你在插入語句之前檢查'if ...'是否在你的if語句中兩次'$ PlaceLink!= NULL' ... – IzzEps

+0

謝謝。我解決了這個問題。這會導致問題嗎? – Austin

+0

不太可能...您確認您的數據庫連接正常工作嗎? – IzzEps

回答

0

你的問題幾乎可以肯定與請求進來,但這裏有幾個問題,你可以解決你的代碼。

  • htmlspecialchars()不適用於插入到數據庫中。當你想以HTML的形式顯示某些東西時使用它。
  • 您檢查的這些值都不會爲空,因爲您通過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

此外,我假設您使用的是當前版本的PHP,支持null coalesce operator;如果不是這種情況,您需要將$foo = $bar ?? null替換爲$foo = isset($bar) ? $bar : null

還有一點,如果系統上的每個用戶在用戶表中都有一個條目,那麼您應該在PlayerBans表中真正擁有用戶ID和SenderID列,這些列是外鍵返回給用戶表的。如果您經常查詢此列,那麼比使用非結構化文本列更有意義。

相關問題