1

我正在尝试使用 php 后端创建一个角度应用程序,现在我遇到了一个问题。我有一个 php 代码,我将把它粘贴在这里。

<?php
    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    $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);
?>

现在我添加了一个静态 json 作为输入数据。我看到 angular 在 http get 和 post 之后返回一个值,所以我想要实现的是返回带有添加到数据库中的 ID 的 json。我用返回contact_id的SQL重新运行这个id,我想把它插入到json中,但我不知道怎么做。在我将该项目插入 json 后,我希望它看起来像这样

{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}

实际上要添加"contact_id":"1"到json中。谢谢你,丹尼尔。

4

2 回答 2

1

这个对我有用。

<?php

    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    /*
    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);

结果 :

{"contact_id":"1","contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}

根据您的代码:

<?php
    $textdata = '{"contact_firstname":"Daniel","contact_lastname":"Pacuraru","contact_email":["pacurarudaniel@gmail.com","hello@pacurarudaniel.com"]}';
    $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);
?>
于 2013-10-04T19:57:01.123 回答
0

删除行:

$json_item = 'contact_id' => addslashes($row['contact_id']);
array_push($json, $json_item);

添加行:

$json_item = array('contact_id' => addslashes($row['contact_id']));
$json = $json_item + $json;

它将contact_id 获取到第一个索引。但是,如果您希望它在最后一个索引上,则忽略上面的行并执行以下操作:

$json['contact_id'] = addslashes($row['contact_id'])
于 2013-10-04T19:34:38.533 回答