Reputation: 1061
I used this code to decode json, sent with curl (POST):
$json_obj = json_decode( file_get_contents('php://input')); // JSON as obj
var_dump($json_obj);
$id_a = $json_obj -> {'$id_a'};
$id_b = $json_obj -> {'$id_b'};
$value = $json_obj -> {'$value'};
It should works fine, because var_dump() says:
object(stdClass)#1 (3) {
["id_a"]=>
int(1)
["id_b"]=>
int(1)
["value"]=>
float(89.35)
}
Then I use these vars to make a sql query:
$sqlcmd = "INSERT INTO TABLE_1 (ID_A, ID_B, VALUE)
VALUES (".$id_a.", ".$id_b.", ".$value.")";
I pass it to mysql_query(), but here it is how it reads $sqlcmd:
INSERT INTO TABLE_1 (ID_A, ID_B, VALUE)
VALUES (, , )
Void values...
Any hint? Where I am mistaking? Thanks in advance
Upvotes: 0
Views: 89
Reputation: 1544
You have extra $ signs in your code such as:
$id_a = $json_obj -> {'$id_a'};
Try this instead:
$id_a = $json_obj -> {'id_a'};
$id_b = $json_obj -> {'id_b'};
$value = $json_obj -> {'value'};
Upvotes: 0
Reputation: 43770
You aren't setting the variables correctly.
$id_a = $json_obj->id_a;
$id_b = $json_obj->id_b;
$value = $json_obj->value;
Upvotes: 1