JSON 到 PHP,然后是 foreach?

发布于 2024-08-18 17:23:22 字数 908 浏览 5 评论 0原文

我从 jQueryscript 向 PHP 页面发送一个有效的 JSON 字符串:

var data = '{
 "data":[
  {
   "id":"12",
   "checked":"true"
  },{
   "id":"4",
   "checked":"false"
  },{
   "id":"33",
   "checked":"false"
  }
 ]
}';


$.post ("page.php", { data_input:data }, function (data) {
 // code
});

在 PHP 页面中获取数据后,我使用 json_decode 方法解析它,然后尝试在 foreach 中使用它为 PDO 查询创建的 语句:

<?php

$data_input = json_decode ($_REQUEST['data_input'], true);

$sql = "UPDATE my_table SET user_enabled = :checked WHERE node_prop_id = :id";
$stmt = $dns->prepare ($sql);

foreach ($data_input as $data) {
 $ok = $stmt->execute ($data);
 $num = $stmt->rowCount ();
} 
if ($ok) return 1;
else return 0;

?>

这会返回错误:

PHP 警告:为 /home/.../page.php 第 XX 行的 foreach() 提供的参数无效

我可以找到在 foreach 语句中使用 JSON 数据的方法吗?

I send a valid JSON string to my PHP page from jQueryscript:

var data = '{
 "data":[
  {
   "id":"12",
   "checked":"true"
  },{
   "id":"4",
   "checked":"false"
  },{
   "id":"33",
   "checked":"false"
  }
 ]
}';


$.post ("page.php", { data_input:data }, function (data) {
 // code
});

Once I get the data in my PHP page, I parse it with the json_decode method, and then try to use it in a foreach statement created for a PDO query:

<?php

$data_input = json_decode ($_REQUEST['data_input'], true);

$sql = "UPDATE my_table SET user_enabled = :checked WHERE node_prop_id = :id";
$stmt = $dns->prepare ($sql);

foreach ($data_input as $data) {
 $ok = $stmt->execute ($data);
 $num = $stmt->rowCount ();
} 
if ($ok) return 1;
else return 0;

?>

This returns me the error:

PHP Warning: Invalid argument supplied for foreach() in /home/.../page.php on line XX

Can I find a way to use my JSON data in the foreach statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青柠芒果 2024-08-25 17:23:22

返回值

以适当的 PHP 类型返回以 json 编码的值。 true、false 和 null(不区分大小写)值分别返回为 TRUE、FALSE 和 NULL。如果json无法解码或者编码数据深度超过递归限制,则返回NULL。

尽管就你而言,应该是这样。我打赌解码错误。

编辑: Victor Nicollet 发现了实际的错误。使用 json_last_error 并多做一些检查还是不错的!

Return value

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

though in your case, it should be. I am betting on a decoding error.

Edit: Victor Nicollet spotted the actual error. It's still good to use json_last_error and do some more checks!

谜兔 2024-08-25 17:23:22

您将 $_REQUEST['data'] 视为 JSON 字符串,但情况并非总是如此(如果我愿意,我可以发送具有无效值的请求),也不会它始终是表示数组或字典的 JSON 字符串。您需要事先检查,如果没有,请做出相应反应。

现在,针对您的实际错误。您写道:

 $.post (page.php, { data_input:data }, function (data) {
   // code
 });

这将是一个错误,因为 page.php 没有被引用。但即使假设它在您的实际代码中,服务器端数据也会存储在 $_POST['data_input'] 中,而不是 $_POST['data']

You're treating $_REQUEST['data'] as if it were a JSON string, which is not always the case (I could send a request with an invalid value if I wanted to), nor will it always be a JSON string representing an array or dictionary. You would need to check beforehand, and react accordingly if it isn't.

Now, for your actual bug. You wrote:

 $.post (page.php, { data_input:data }, function (data) {
   // code
 });

This would be an error, since page.php is not quoted. But even assuming that it is in your actual code, server-side the data would be stored in $_POST['data_input'], not $_POST['data'].

暖伴 2024-08-25 17:23:22

我发现了问题:

<?php

// I've changed $data to $json for more clarity
$json = json_decode (stripslashes ($_REQUEST['json_string']), true); // added stripslashes method for more debug, but i think it still works without
$json = $json["data"]; // this was the problem

$sql = "UPDATE my_table SET user_enabled = :checked WHERE node_prop_id = :id";
$stmt = $dns->prepare ($sql);

foreach ($json as $value) {
    $ok = $stmt->execute ($value);
    $num = $stmt->rowCount ();
} 
if ($ok) return 1;
else return 0;

?>

I found the problem:

<?php

// I've changed $data to $json for more clarity
$json = json_decode (stripslashes ($_REQUEST['json_string']), true); // added stripslashes method for more debug, but i think it still works without
$json = $json["data"]; // this was the problem

$sql = "UPDATE my_table SET user_enabled = :checked WHERE node_prop_id = :id";
$stmt = $dns->prepare ($sql);

foreach ($json as $value) {
    $ok = $stmt->execute ($value);
    $num = $stmt->rowCount ();
} 
if ($ok) return 1;
else return 0;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文