【问题标题】:Sending string with json to php将带有json的字符串发送到php
【发布时间】:2012-07-27 11:56:48
【问题描述】:

我的代码运行良好,除了“userName”,由于某种原因,通过 JSON 发送字符串不会发布到表,它什么也不发送。

谁能看出问题所在?

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

【问题讨论】:

  • 我看不到$obj的任何用法。
  • 我想你把这一切都搞混了......应该发送 1 个 JSON 对象而不是 3 个?它应该类似于$obj=json_decode($_GET['jsonObj']) 以及从 JSON 对象中获取的其余值。另一件事......你没有清理你的输入并将其直接输入你的 MySQL 数据库。
  • 嗯,好的。感谢您的提示。我还是有点新,所以现在就开始工作吧。
  • 在这里看看我的回答:stackoverflow.com/a/11606240/1031312
  • @user1555800 这实际上只是对 getJSON 的混淆。 jquery 中的 getJSON 表示“执行 GET 请求,我希望服务器的结果是 JSON 格式”。响应服务器应将数据作为标准 GET 请求(即 /something?param1=yes&param2=25)处理,并且是预期的发回 json (所以你最后的 json_encode 是正确的,但发送到服务器的数据是标准的 GET 数据,而不是 json)

标签: php mysql ajax json


【解决方案1】:

你为什么用这个:

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

正常工作

$userName = $_GET['userName'];

【讨论】:

  • 好的,我纠正了自己,这就是解决方案。 json_decode 是干什么用的?
  • @user1555800 看看这个:php.net/manual/en/function.json-decode.php 另外,必须提到您的代码目前很容易受到 SQL 注入的影响......
  • 如果变量 $lowestScoreId 包含以下字符串“' OR '1' = '1”。 'OR' 后跟的条件始终为真,因此可以对表的所有行执行更新。 mysql_query("UPDATE highScores SET name = '$userName', score = '$userPoints', date = '$currentTime' WHERE id='' OR '1' = '1'");
猜你喜欢
  • 2018-10-09
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多