Reputation: 1205
I am trying to create an angular app with php backend and right now I am stuck with a problem. I have a php code which I will paste it here.
<?php
$textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["[email protected]","[email protected]"]}';
$json = json_decode($textdata, true);
//$json = json_decode(file_get_contents('php://input'), true);
$dbhost = "localhost";
$dbport = "5432";
$dbname = "fixevents";
$dbuser = "postgres";
$dbpass = "123";
$connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);
$query = "INSERT INTO contact (contact_firstname, contact_lastname, contact_email) VALUES ('" . addslashes($json['contact_firstname']) . "', '" . addslashes($json['contact_lastname']) . "', '" . addslashes(implode(',,,', $json['contact_email'])) . "') RETURNING contact_id";
$result = pg_query($connect, $query);
$row = pg_fetch_array($result);
$json_item = 'contact_id' => addslashes($row['contact_id']);
array_push($json, $json_item);
echo json_encode($json);
?>
For now I added a static json to it as an input data. I saw that angular returns a value after http get and post so what I want to achieve is to return the json with the ID that was added into the database. I retrun this id with SQL returning contact_id, and I want to insert it into the json but I have no idea how. After I insert that item into json I want it to look like this
{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["[email protected]","[email protected]"]}
Practically to add "contact_id":"1"
into the json. Thank you, Daniel.
Upvotes: 1
Views: 169
Reputation: 513
It works for me.
<?php
$textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["[email protected]","[email protected]"]}';
/*
Finding contact_id ...
$contact_id = 1; // example
*/
$contact_id = 1;
$json = json_decode($textdata,true);
$json = array('contact_id'=>addslashes($contact_id)) + $json;
echo json_encode($json);
The result :
{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["[email protected]","[email protected]"]}
Based on your code :
<?php
$textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["[email protected]","[email protected]"]}';
$json = json_decode($textdata, true);
//$json = json_decode(file_get_contents('php://input'), true);
$dbhost = "localhost";
$dbport = "5432";
$dbname = "fixevents";
$dbuser = "postgres";
$dbpass = "123";
$connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);
$query = "INSERT INTO contact (contact_firstname, contact_lastname, contact_email) VALUES ('" . addslashes($json['contact_firstname']) . "', '" . addslashes($json['contact_lastname']) . "', '" . addslashes(implode(',,,', $json['contact_email'])) . "') RETURNING contact_id";
$result = pg_query($connect, $query);
$row = pg_fetch_array($result);
$json_item = array('contact_id' => addslashes($row['contact_id']));
$json = $json_item + $json;
echo json_encode($json);
?>
Upvotes: 1
Reputation: 347
Remove line:
$json_item = 'contact_id' => addslashes($row['contact_id']);
array_push($json, $json_item);
Add line:
$json_item = array('contact_id' => addslashes($row['contact_id']));
$json = $json_item + $json;
It will get the contact_id to the first index. But if you want it on last index then ignore lines above and do:
$json['contact_id'] = addslashes($row['contact_id'])
Upvotes: 0