2016-07-26 24 views
0

PHP版本7.0我是否錯誤地使用了JSON_decode或php內容?

所以基本上有另一個網站發送信息到這個腳本。該腳本未連接到網站的任何其他部分。因此,我不查看此頁面。信息是以編碼的JSON格式發送的,恐怕我試圖錯誤地解碼,或者它沒有正確接收信息。

代碼

<?php 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 
include '../includes/connection.php'; 
$array = json_decode(file_get_contents("php://input"), true); 
$UserID = $array['UserID']; 
$CallerID = $array['CallerID']; 
$Report = $array['Report']; 
$Violator = $array['Violator']; 
$PlaceLink = $array['PlaceLink']; 

if($CallerID !=NULL and $Report != NULL and $PlaceLink != NULL){ 
    $query = $handler->prepare("INSERT INTO Calls (`ID`, `UserID`,`CallerID`, `Report`, `Violator`, `PlaceLink`, `Status`) VALUES (NULL, :userID, :CallerID, :Report, :Violator, :PlaceLink, '1')"); 
    $query->bindParam(':userID', $UserID); 
    $query->bindParam(':CallerID', $CallerID); 
    $query->bindParam(':Report', $Report); 
    $query->bindParam(':Violator', $Violator); 
    $query->bindParam(':PlaceLink', $PlaceLink); 
    $query->execute(); 
} 
?> 

預期結果:從其他網站發送的信息被插入到我的桌子底下數據庫「調用」。

結果:沒有任何反應。

如果我轉到頁面,則不顯示任何內容。這是因爲根本原因沒有HTML。我實際上並未將其用作網頁。我用它作爲腳本。

+0

爲什麼你認爲這是不正確的?請解釋這個問題。 –

+0

你可以看到它應該將信息插入到數據庫中,而不是。我知道連接和處理程序正在工作,因爲它們也在其他地方使用。 – Austin

+0

歡迎來到Stack Overflow!你能詳細說明你的代碼「不起作用」嗎?你在期待什麼,究竟發生了什麼?如果您遇到異常/錯誤,請發佈其發生的行和異常/錯誤的詳細信息。請[編輯]這些細節或我們可能無法提供幫助。 – FrankerZ

回答

0

代碼

<?php 
ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 
include '../includes/connection.php'; 
$array = json_decode(file_get_contents("php://input"), true); 
$UserID = $array['UserID']; 
$CallerID = $array['CallerID']; 
$Report = $array['Report']; 
$Violator = $array['Violator']; 
$PlaceLink = $array['PlaceLink']; 
echo "<script>alert(". $array .");</script>"; 

if($CallerID !=NULL and $Report != NULL and $PlaceLink != NULL){ 
    $query = $handler->prepare("INSERT INTO Calls (`ID`, `UserID`,`CallerID`, `Report`, `Violator`, `PlaceLink`, `Status`) VALUES (NULL, :userID, :CallerID, :Report, :Violator, :PlaceLink, '1')"); 
    $query->bindParam(':userID', $UserID); 
    $query->bindParam(':CallerID', $CallerID); 
    $query->bindParam(':Report', $Report); 
    $query->bindParam(':Violator', $Violator); 
    $query->bindParam(':PlaceLink', $PlaceLink); 
    $query->execute(); 
} 
?> 
+0

所以我假設我需要導航到我的瀏覽器中的腳本,等待,然後發送信息並查看警報是否出現? – Austin

+0

如果您的php位於「folderpath/index.php」中,請將其放置在瀏覽器的搜索框中。隨着該json數據準備就緒。這是(php://輸入),如果需要,用示例數據替換它 – PSo

+0

好吧,所以基本上這是它的工作原理。該腳本位於/scripts/addCall.php |下在另一個網站上,當按下某個按鈕時,它會對其信息(由客戶端收集)進行編碼並將其發送到此腳本。爲了澄清,我繼續前進並導航到此腳本。在頁面上,繼續併發送數據? – Austin

相關問題