0

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

user2452426
  • 991
  • 1
  • 7
  • 7

2 Answers2

1

You aren't setting the variables correctly.

$id_a = $json_obj->id_a; 
$id_b =  $json_obj->id_b;
$value = $json_obj->value;
Schleis
  • 41,516
  • 7
  • 68
  • 87
0

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'};
Eric Wich
  • 1,504
  • 10
  • 8