dougou8573 2013-09-05 04:34
浏览 40
已采纳

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

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>"}

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.

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:

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!! While $in1 yields an empty field!!

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.

$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.

Non-working output Working output

The only difference between these two is the result of $stmt->rowCount(). The sample that correctly updates the field says int(0) and the one that empties it says 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();
}
  • 写回答

1条回答 默认 最新

  • dptn69182 2013-09-05 07:54
    关注

    Massive sigh. The issue is with my javascript, not php. I was testing this out in my api, which several things make calls to throughout my somewhat large app. 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. 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. More unfortunate is that the success of the mysqli threw me even further off, solidifying in my mind that the issue was with PDO. It turns out that it was just changing which ajax call was responded to last.

    And thus, I have hopefully learned not to have so much tunnel-vision when going through the debugging process.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染