Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用PDO将行插入PGSQL_Php_Postgresql_Pdo_Prepared Statement - Fatal编程技术网

Php 使用PDO将行插入PGSQL

Php 使用PDO将行插入PGSQL,php,postgresql,pdo,prepared-statement,Php,Postgresql,Pdo,Prepared Statement,我无法让php postgre使用以下代码从php数组中插入一行new\u address: $customer_id = '2319'; $use_frequency = 1; $sth = $dbh->prepare(" INSERT INTO address ('storeid', 'classtypeid', 'modifiedbyuser',

我无法让php postgre使用以下代码从php数组中插入一行
new\u 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',^'在

print\r($new\u 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
)
)
感谢您的建议!

根据,您必须用双引号转义列名

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

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

insert into address
    ("streetaddress1", 
     "streetaddress2", 
     "city", 
     "state", 
     ...)
values (:address_1, 
       :address_2, 
       :city, 
       :state, 
       ...)
从您的评论中,您必须修改
$new\u 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;
    }
}
根据,必须用双引号转义列名

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

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

insert into address
    ("streetaddress1", 
     "streetaddress2", 
     "city", 
     "state", 
     ...)
values (:address_1, 
       :address_2, 
       :city, 
       :state, 
       ...)
从您的评论中,您必须修改
$new\u 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;
    }
}

谢谢。我已经更改了。我现在得到错误:
致命错误:未捕获异常'PDOException',消息'SQLSTATE[42601]:语法错误:7错误:输入行末尾的语法错误48:$9^'
@thomas这将是最后一行。在您的语句中添加结束语(
。啊哈。伟大的我现在(当然)得到了以下错误:`sequence address\u id\u seq1'
。这很奇怪。因为我没有名为
address\u id\u seq
的列。上一个自动递增
id`column可能有问题吗?@thomas我不知道。当然,这取决于您如何定义
序列地址\u id\u seq1
地址
。我只是从来没有创建过名为
地址\u id\u seq1
的东西。我使用phpPgAdmin来设置数据库和特定的表。我可以在以后定义序列吗?谢谢。我已经改变了。我现在得到了错误:
致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42601]:语法错误:7错误:输入行末尾的语法错误48:$9^”
@thomas这将是最后一行。在对账单中添加一个结束语(
。啊哈。伟大的我现在(当然)得到了以下错误:`sequence address\u id\u seq1'
。这很奇怪。因为我没有名为
address\u id\u seq
的列。上一个自动递增
id`column可能有问题吗?@thomas我不知道。当然,这取决于您如何定义
序列地址\u id\u seq1
地址
。我只是从来没有创建过名为
地址\u id\u seq1
的东西。我使用phpPgAdmin来设置数据库和特定的表。我可以在以后定义序列吗?