Wednesday, 4 September 2013

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

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>"}
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.
$params = [':item' => 2, ':in'=>$in2];
$query = "UPDATE plugin_instances SET config=:in WHERE id=:item";

No comments:

Post a Comment