php - insert row into PGSQL USING PDO -
i cannot php postgre insert row php array new_address
, using code:
$customer_id = '2319'; $use_frequency = 1; $sth = $dbh->prepare(" insert 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 lastly column in table id
, serial
have omitted it, thinking auto-increment, please tell me if i'm wrong.
i getting error:
fatal error: uncaught exception 'pdoexception' message 'sqlstate[42601]: syntax error: 7 error: syntax error @ 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 illustration 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] => ) )
thanks advice!
according 4.1. lexical structure, must escape column names double quotes "
there sec kind of identifier: delimited identifier or quoted identifier. formed enclosing arbitrary sequence of characters in double-quotes ("). delimited identifier identifier, never key word. "select" used refer column or table named "select", whereas unquoted select taken key word , hence provoke parse error when used table or column name expected.
insert address ("storeid", "classtypeid", ...
additionally, if set default values columns null
, can omit them column list , utilize need
insert address ("streetaddress1", "streetaddress2", "city", "state", ...) values (:address_1, :address_2, :city, :state, ...)
from comment, must modify $new_address
array. indexed numerically , not name.
if can alter json
{ "customer_id": 9319, "address_1": "1515 illustration trail", "address_2": "box 1", "city": "town city", "state": "mi", "zip": "12345", "country": "us" }
you can utilize
$new_address = json_decode($json, true);
to associative array.
if cannot alter json, must map associative array
$json = json_decode('[ { "customer_id": 9319 }, { "address_1": "1515 illustration trail" }, { "address_2": "box 1" }, { "city": "town city" }, { "state": "mi" }, { "zip": "12345" }, { "country": "us" } ]'); foreach ($json $element) { foreach ($element $key => $val) { $new_address[$key] = $val; } }
php postgresql pdo prepared-statement
No comments:
Post a Comment