简体   繁体   English

PHP PDO将javascript对象(ajax)中的字符串保存为空字段

[英]PHP PDO is saving string from javascript object (ajax) as empty field

This is interesting and by that I mean incredibly frustrating. 这很有趣,我的意思是令人沮丧。

I am passing this data to my php file via an ajax call: {"html":"<div>I'm a div!!</div>"} 我通过ajax调用将这些数据传递到我的php文件中: {"html":"<div>I'm a div!!</div>"}

I want to preface this next statement by saying that I do understand the reasons for not saving json to a database, but it does have a use here. 我想在接下来的声明中开头,说我确实理解了不将json保存到数据库的原因,但是它确实在这里有用。

When I save this data to the database field, the field is empty. 当我将此数据保存到数据库字段时,该字段为空。 Now see this: 现在看到这个:

$in1 = file_get_contents('php://input'); //from ajax
var_dump($in1);
$in2 = '{"html":"<div>I\'m a div!!</div>"}';
var_dump($in2);

value of my ajax call: 我的ajax调用的价值:

string(33) "{"html":"<div>I'm a div!!</div>"}"
string(33) "{"html":"<div>I'm a div!!</div>"}"

Perfectly the same! 完全一样! Yet, $in2 will save to the database just fine!! 但是, $in2可以很好地保存到数据库中!! While $in1 yields an empty field!! 虽然$in1产生一个空字段!

To be certain, consider this: 可以肯定的考虑一下:

if ($in1 === $in2) { echo "They're equal!"; }

Go figure...they're exactly equal, yet one will save correctly and the other won't. 走吧……他们完全相等,但是一个会正确保存,而另一个不会。 Amazing. 惊人。

Further: mysqli does not have this issue, so that narrows it down to being a PDO issue. 此外: mysqli没有此问题,因此将其范围缩小为PDO问题。

$query = "UPDATE plugin_instances SET config=(?) WHERE id=2";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $in1);
$stmt->execute(); //correct value in the db!!

I have now removed everything and this is the whole php file. 我现在删除了所有内容,这是整个php文件。

Non-working output Working output 非工作输出 工作输出

The only difference between these two is the result of $stmt->rowCount() . 两者之间的唯一区别是$stmt->rowCount() The sample that correctly updates the field says int(0) and the one that empties it says int(1) . 正确更新该字段的示例为int(0) ,而清空该字段的示例为int(1)

$db = new PDO('mysql:host=localhost;dbname=disarray', 'root', 'temp');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
var_dump($db);

$params = [':foo'=>'{"html":"<div>I\'m a div!!</div>"}'];
var_dump($params);
$params = [':foo'=>file_get_contents('php://input')];
var_dump($params);

$query = "UPDATE plugin_instances SET config=:foo WHERE id=2";
var_dump($query);

try {
  $stmt = $db->prepare($query);
  var_dump($stmt);
  $stmt->execute($params);
  var_dump($stmt);
  var_dump($stmt->rowCount());
}
  catch (PDOException $e) {
  echo $e->getMessage();
}

Massive sigh . 巨大的叹息 The issue is with my javascript, not php. 问题出在我的JavaScript,而不是php。 I was testing this out in my api, which several things make calls to throughout my somewhat large app. 我在api中对此进行了测试,其中有几件事会在整个大型应用中进行调用。 There was an additional ajax call being made that I was unaware of (it was left in the code by error) and it wasn't sending any data, so my test script in the api was running with no data and thus emptying the database field right after I wrote to it. 我没有意识到另外一个ajax调用(它被错误地留在了代码中)并且没有发送任何数据,因此我在API中的测试脚本没有任何数据运行,因此清空了数据库字段在我写信之后。 Of course, entering the data directly into the script worked out great because both calls were doing the same thing. 当然,直接将数据输入脚本非常有效,因为两个调用都在做相同的事情。

It is unfortunate that when I stripped down my php code, I used the same filename/location. 不幸的是,当我删除我的php代码时,我使用了相同的文件名/位置。 More unfortunate is that the success of the mysqli threw me even further off, solidifying in my mind that the issue was with PDO. 更不幸的是, mysqli的成功使我mysqli使我mysqli问题出在PDO。 It turns out that it was just changing which ajax call was responded to last. 事实证明,这只是在更改最后一个响应的ajax调用。

And thus, I have hopefully learned not to have so much tunnel-vision when going through the debugging process. 因此,我希望已经学会了在调试过程中没有太多的隧道视觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM