使用PDO将行插入PGSQL

时间:2013-02-12 23:30:36

标签: php postgresql pdo prepared-statement

我无法让php postgre使用以下代码从php数组new_address插入一行:

    $customer_id = '2319';
    $use_frequency = 1;

    $sth = $dbh->prepare("
    INSERT INTO address 
            ('storeid', 
             'classtypeid', 
             'modifiedbyuser', 
             'modifiedbycomputer', 
             'modifieddate', 
             'seqid', 
             'issystem', 
             'isactive', 
             'streetaddress1', 
             'streetaddress2', 
             'city', 
             'state', 
             'county', 
             'postalcode', 
             'country', 
             'formattedtext', 
             'taxclassid', 
             'isvalidated', 
             'validatedaddress', 
             'hasvalidationerror', 
             'validationerror', 
             'customer_id', 
             'use_frequency') 
   VALUES   ( NULL, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
             :address_1, 
             :address_2, 
             :city, 
             :state, 
              NULL, 
             :zip, 
             :country, 
             :formatted_text, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
              NULL, 
             :customer_id, 
             :use_frequency");



$sth->execute(array(
    ':address_1' => $new_address['address_1'],
    ':address_2' => $new_address['address_2'],
    ':city' => $new_address['city'],
    ':state' => $new_address['state'],
    ':zip' => $new_address['zip'],
    ':country' =>$new_address['country'],
    ':formatted_text' => $formatted_text,
    ':customer_id' => $customer_id, 
    ':use_frequency' => $use_frequency
    );           


$sth->execute();

表格中的最后一列是id,它是serial所以我省略了它,认为它会自动递增,但请告诉我,如果我错了。

我收到错误:

  

致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [42601]:语法错误:7错误:语法错误在或附近   “'storeid'”第3行:('storeid',^'in

print_r($new_address);告诉我:

Array (
[0] => stdClass Object (
[customer_id] => 9319
)
[1] => stdClass Object (
[address_1] => 1515 example st
)
[2] => stdClass Object (
[address_2] => box 1
)
[3] => stdClass Object (
[city] => town
)
[4] => stdClass Object (
[state] => ST
)
[5] => stdClass Object (
[zip] => 12345
)
[6] => stdClass Object (
[country] => US
)
)

感谢您的任何建议!

1 个答案:

答案 0 :(得分:1)

根据4.1. Lexical Structure,您必须使用双引号"

来转义列名称
  

还有第二种标识符:分隔标识符或带引号的标识符。它是通过用双引号(“)括起任意字符序列而形成的。分隔标识符始终是标识符,绝不是关键字。因此,”select“可用于引用名为”select“的列或表,而不带引号的选择将被视为关键字,因此在预期使用表或列名称时会引发解析错误。

INSERT INTO address 
        ("storeid", 
         "classtypeid", 
         ...

此外,如果您将列的默认值设置为NULL,则可以从列列表中省略它们,并仅使用您真正需要的列

insert into address
    ("streetaddress1", 
     "streetaddress2", 
     "city", 
     "state", 
     ...)
values (:address_1, 
       :address_2, 
       :city, 
       :state, 
       ...)

根据您的评论,您必须修改$new_address数组。它按数字索引,按名称。

如果您可以将JSON更改为

{ "customer_id": 9319,
  "address_1": "1515 example trail",
  "address_2": "box 1",
  "city": "town city",
  "state": "MI",
  "zip": "12345",
  "country": "US" }

你可以使用

$new_address = json_decode($json, true);

获取关联数组。

如果无法更改JSON,则必须将其映射到关联数组

$json = json_decode('[ { "customer_id": 9319 }, { "address_1": "1515 example trail" }, { "address_2": "box 1" }, { "city": "town city" }, { "state": "MI" }, { "zip": "12345" }, { "country": "US" } ]');

foreach ($json as $element) {
    foreach ($element as $key => $val) {
        $new_address[$key] = $val;
    }
}