2015-06-28 36 views
4

我收到JSON數據後....處理JSON的數據後在PHP

{"split_info":"17076370","customerName":"Lahoti","status":"failed","error_Message":"fail.","paymentId":"17076370","productInfo":"productInfo","customerEmail":"[email protected]","customerPhone":"999999999","merchantTransactionId":"BR121","amount":"19.0","notificationId":"443"} 

我已經寫PHP代碼更新使用所接收的merchantTransactionId作爲JSON數據後我的數據庫。 我的數據庫是不會更新... 我的PHP代碼如下 請幫助..

<?php 
include("dbconnection.php"); 
if(isset($_POST)) 
{ 
$json_a = json_decode($_POST, true); 
$Id=$json_a['merchantTransactionId']; 
$status="payUMoney"; 
mysql_query("UPDATE std SET status= '".$payStatus."' WHERE Id='".$Id."'", $db); 
?> 
+2

這是錯誤的語法,但你也可以考慮使用PDO或mysqli的API –

+0

'$ status'和'$ payStatus'?另外你很容易受到[sql注入攻擊](http://bobby-tables.com) –

+0

如果你發佈你的json數據作爲郵件正文而不是表單值,那麼你需要通過$ json_string來訪問它= file_get_contents('php:// input') eg http://stackoverflow.com/questions/8893574/php-php-input-vs-post –

回答

-1
$json_a = json_decode($_POST, true); 

$ _ POST是一個數組

json_decode()接受字符串

的var_dump($ _ POST),看看你的JSON數據位於

2

您必須閱讀的HTTP請求正文目錄$ _POST只提供表單數據,從你試圖接收JSON /應用程序請求的聲音開始?

<?php 
include("dbconnection.php"); 

$json_string = file_get_contents('php://input'); 

if($json_string !== null) 
{ 
$json_a = json_decode($json_string, true); 
$Id=$json_a['merchantTransactionId']; 
$status="payUMoney"; 
mysql_query("UPDATE std SET status= '".$payStatus."' WHERE Id='".$Id."'", $db); 
} 
?> 

請永遠不要生成SQL語句,雖然這樣一來,他們大量不安全的,將離開你打開到SQL注入攻擊和各種髒東西的。