2010-09-27 156 views
0

g,一天。有人可以幫助我理解爲什麼我的代碼不會將結果返回給json嗎?我確信在我的代碼中有一個錯誤,但似乎無法找到它。應該發生的是$ dept和$ box的值應該在警報中返回,但這不會發生。感謝json不返回數據

<?php 

function runSQL($rsql) { 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "sample"; 
$connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database"); 
$db = mysql_select_db($dbname); 
$result = mysql_query($rsql) or die ('test'); 
return $result; 
mysql_close($connect); 
} 
$new = 1; 

$items = rtrim($_POST['items'],","); 
$sql = "SELECT * FROM `boxes` WHERE id IN ($items)"; 
$result = runSQL($sql); 


$i = 0; 
$rows = mysql_num_rows($result); 
while ($row = mysql_fetch_array($result)) { 
    if ($i < $rows) { 

     $dept .= $row['department'] . "," ; 
     $box .= $row['custref'] . "," ; 
    } else { 

    $dept .= $row['department']; 
    $box .= $row['custref']; 
    } 
    $i++; 
} 


/*$items = rtrim($_POST['items'],","); 
$sql = "UPDATE `boxes` SET status = 'Deleted' WHERE id IN ($items)"; 
$result = runSQL($sql);*/ 

//$sql = "INSERT INTO `act` (`item`) VALUES (\''.$box.'\')"; 
//$result = runSQL($sql); 

$total = count(explode(",",$items)); 
$result = runSQL($sql); 
$total = mysql_affected_rows(); 
/// Line 18/19 commented for demo purposes. The MySQL query is not executed in this case. When line 18 and 19 are uncommented, the MySQL query will be executed. 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache"); 
header("Content-type: text/x-json"); 
$json = ""; 
$json .= "{\n"; 
$json .= "dept: '".$dept.",'\n"; 
$json .= "box: '".$box."'\n"; 
$json .= "}\n"; 
echo $json; 
?> 

阿賈克斯

success: function(data){ 
dept = data.dept; 
box = data.box; 
alert("You have successfully deleted\n\r\n\rBox(es): "+data.dept+data.box); 
$("#flex1").flexReload(); 
    } 
+1

** **爲什麼你產生JSON自己,而不是使用幾十個擴展或庫那會爲你的嗎? – 2010-09-27 13:30:06

+0

習慣:-)我真的因爲時間限制需要在這段代碼中找到錯誤。謝謝 – 2010-09-27 13:34:05

回答

0

你JSON是不正確。它的格式是這樣的:

{ 
dept: '...' 
box: '...' 
} 

它應該是:

{ 
"dept": "..." 
"box": "..." 
} 

標識符需要他們周圍引號,並且字符串用引號括起來,而不是單引號。

0

RFC4627將JSON的媒體類型定義爲「application/json」。

1
$sql = "SELECT * FROM `boxes` WHERE id IN ($items)"; 

SQL注入漏洞。如果他們是字符串,您必須每個單獨的項目mysql_real_escape_string,或者確保他們只是數字,如果這是他們應該是(例如與intval())。或使用參數化查詢。

header("Content-type: text/x-json"); 

application/json

$json .= "dept: '".$dept.",'\n"; 

除了JSON需要圍繞鍵和字符串值雙引號,您還需要爲JavaScript字符串字面轉義值被注入到一個字符串。否則,一個撇號/ quote/backslash/newline會破壞字符串。你主要可以通過addslashes()來做到這一點。

但實際上,沒有調用構建自己的JSON值(或其他JavaScript文本)。 PHP給你json_encode()。它更簡單,更快,更可靠。用它。

echo json_encode(array(
    'dept'=>$dept, 
    'box'=>$box 
)); 
+0

bobince。我已將代碼更改爲: – 2010-09-27 13:53:45

+0

header(「Content-type:application/json」); $ json。=「dept:'」。$ dept。」,「\ n」個; echo json_encode(array( 'dept'=> $ dept, 'box'=> $ box ));它會導致錯誤:致命錯誤:調用未定義的函數:json_encode()我使用的是php4.4.7 – 2010-09-27 13:54:28

+0

Eek!是的,PHP4沒有'json_encode'恐怕。您確實需要升級:PHP4不再受支持(並且現在還不是很長一段時間);自那時以來,已經有很多安全漏洞被修復。 – bobince 2010-09-27 14:11:05

0

你寫

$json .= "dept: '".$dept.",'\n"; 

所以, 1.你應該鍵和值加雙引號。
2.關鍵值結束後沒有逗號(,)。你在報價中加入它。即您的代碼將創建此json,

dept: 'department,' 

查看逗號的地方。

試試這個:

$json .= "\"dept\": \"".$dept."\",\n";