【问题标题】:insert row into PGSQL USING PDO使用 PDO 将行插入 SQL
【发布时间】:2013-01-28 10:13:32
【问题描述】:

我无法让 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'" LINE 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
)
)

感谢您的建议!

【问题讨论】:

    标签: php postgresql pdo prepared-statement


    【解决方案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;
        }
    }
    

    【讨论】:

    • 谢谢。我已经改变了。我现在得到错误:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input LINE 48: $9 ^'
    • @thomas 这将是最后一行。在您的陈述中添加一个结束括号)
    • 啊哈。伟大的。我现在(当然)得到以下错误:`sequence address_id_seq1'. Which is odd. because I don't have a column named address_id_seq. Is it maybe a problem with the last autoincrement id` column?
    • @thomas 我不知道。当然,这取决于您如何定义 sequence address_id_seq1address
    • 我只是从未创建过任何名为address_id_seq1 的东西。我使用 phpPgAdmin 来设置数据库和那个特定的表。以后可以定义顺序吗?
    猜你喜欢
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2019-07-15
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多