2012-07-27 92 views
0

我的代码很好,除了“userName”,由于某些原因通过JSON发送字符串不会发布到表,它什么都不发送。用json发送字符串到php

任何人都可以看到什么问题是?

jQuery的

lowestScoreId = 1; 
userPoints = 50; 
userName = "ted"; 

$.getJSON("functions/updateHighScores.php", {lowestScoreId: lowestScoreId, userPoints: userPoints, userName: userName}, function(data) { 

    $('#notes').text(data.userName); //for testing 

}); 

PHP

lowestScoreId = json_decode($_GET['lowestScoreId']); 
$userName = json_decode($_GET['userName']); 
$userPoints = json_decode($_GET['userPoints']); 

include 'config.php'; 

$currentTime = time(); 

mysql_query("UPDATE highScores 
SET `name` = '$userName', 
    `score` = '$userPoints', 
    `date` = '$currentTime' 
WHERE id='$lowestScoreId'"); 

echo json_encode(array("userName" => $userName)); // for testing 
+0

我看不到'$ obj'的任何用法。 – Leri 2012-07-27 11:58:50

+0

我想你已经把它混淆了......应该有一个JSON对象不是三个发送的?它应该像'$ obj = json_decode($ _ GET ['jsonObj'])'和其他从JSON对象中取得的值。另一件事......你并没有清理你的输入,并直接输入你的MySQL数据库。 – Ozzy 2012-07-27 12:01:07

+0

嗯,好吧。感谢提示。我仍然有点新,所以在这一点上让事情发挥作用。 – user1555800 2012-07-27 12:02:26

回答

2

你为什么这样做:

$userName = $obj = json_decode($_GET['userName']); 

它正常工作

$userName = $_GET['userName']; 
+0

好吧,我纠正了我的自我,那就是解决方案。什么是json_decode? – user1555800 2012-07-27 12:39:40

+0

@ user1555800看看这:http://php.net/manual/en/function.json-decode.php另外,强制性提及您的代码容易受到SQL注入,因为它目前... – Mansfield 2012-07-27 12:42:45

+0

好吧,它如何易受攻击? – user1555800 2012-07-27 12:54:28