2013-02-12 117 views
1

我不能讓PHP postgre從PHP陣列new_address執行插入一排,使用此代碼:插入行PGSQL使用PDO

$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所以我省略了它,認爲它會自動遞增,但請告訴我,如果我錯了。

我收到錯誤:

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);顯示我:

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

4.1. Lexical Structure,你必須用雙引號"

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", 
     ... 

此外逃脫的列名,如果爲列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; 
    } 
} 
+0

感謝。我改變了這一點。我現在得到錯誤:致命錯誤:帶有消息'SQLSTATE [42601]的未捕獲異常'PDOException':語法錯誤:7錯誤:輸入結束時的語法錯誤行48:$ 9 ^'' – 1252748 2013-02-12 23:44:10

+0

@thomas這將是最後一次線。在你的陳述中添加一個結束語「'''。 – 2013-02-12 23:46:57

+0

啊哈。大。我現在(當然)得到以下錯誤:'序列地址_id_seq1'。這很奇怪。因爲我沒有名爲'address_id_seq'的列。這可能是最後一個autoincrement'id'列的問題嗎? – 1252748 2013-02-12 23:52:40