douji0073 2013-02-12 23:30
浏览 86
已采纳

使用PDO将行插入PGSQL

I cannot get php postgre to do an insert a row from a php array new_address, using this code:

    $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();

The last column in the table is id and it is a serial so I have omitted it, thinking it will auto-increment, but please tell me if I'm wrong.

I am getting the error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "'storeid'" LINE 3: ('storeid', ^' in

print_r($new_address); shows me:

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
)
)

Thanks for any advice!

  • 写回答

1条回答 默认 最新

  • dpbrrczhlwbv849228 2013-02-12 23:37
    关注

    According to 4.1. Lexical Structure, you must escape the column names with double quotes "

    There is a second kind of identifier: the delimited identifier or quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier is always an identifier, never a key word. So "select" could be used to refer to a column or table named "select", whereas an unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

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

    Additionally, if you set the default values for the columns to NULL, you can omit them from the column list and use only those you really need

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

    From your comment, you must modify the $new_address array. It is indexed numerically and not by name.

    If you can change the JSON to

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

    you can use

    $new_address = json_decode($json, true);
    

    to get an associative array.

    If you cannot change the JSON, you must map it to an associative array

    $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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么