pg_update

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_update 更新表

说明

pg_update(
    PgSql\Connection $connection,
    string $table_name,
    array $values,
    array $conditions,
    int $flags = PGSQL_DML_EXEC
): string|bool

pg_update() 更新匹配 conditionsvalues 的记录。

如果指定 flags,则 pg_convert() 使用指定 flag 应用于 values

默认情况下 pg_update() 传递原始值。值必须转义或必须在 flags 中指定 PGSQL_DML_ESCAPE flag。PGSQL_DML_ESCAPE 引用并转义参数/标识符。因此,表/列名称变得区分大小写。

注意转义和预处理查询都不能保护 LIKE 查询、JSON、Array、Regex 等,这些参数要根据上下文来处理。即转义/验证值。

参数

connection

PgSql\Connection 实例。

table_name

要更新记录的表名。

values

array,其键是表 table_name 中的字段名称,其值是要更新的匹配行。

conditions

array,其键是表 table_name 中的字段名称,其值是更新行必须满足的条件。

flags

任意数量的 PGSQL_CONV_FORCE_NULLPGSQL_DML_NO_CONVPGSQL_DML_ESCAPEPGSQL_DML_EXECPGSQL_DML_ASYNCPGSQL_DML_STRING 的组合。当 PGSQL_DML_STRINGflags 的一部分,然后将返回查询字符串。当设置了 PGSQL_DML_NO_CONVPGSQL_DML_ESCAPE 时,不会在内部调用 pg_convert()

返回值

成功时返回 true, 或者在失败时返回 false。如果通过 flags 传递 PGSQL_DML_STRING,成功时返回 string

更新日志

版本 说明
8.1.0 现在 connection 参数接受 PgSql\Connection 实例,之前接受 resource

示例

示例 #1 pg_update() 示例

<?php
$db
= pg_connect('dbname=foo');
$data = array('field1'=>'AA', 'field2'=>'BB');
// 在某种程度上安全,因为所有值都转义了。
// 然而 PostgreSQL 支持 JSON/Array。无论是
// 转义还是预处理都不安全。
$res = pg_update($db, 'post_log', $_POST, $data);
if (
$res) {
echo
"Data is updated: $res\n";
} else {
echo
"User must have sent wrong inputs\n";
}
?>

参见

  • pg_convert() - 将关联的数组值转换为适合 SQL 语句的格式

添加备注

用户贡献的备注 3 notes

up
2
dominik dot bonsch at homesono dot de
16 years ago
Using pg_update() and pg_insert() without key validation is not secure!

You need to check which data pairs you get, and if you want to allow to updated this column.

Example:

You have a table with tree colums: username, password, userlevel.

Your users may change only their username, and their password but not their userlevel.

If you don't filter the keys in the request array, every user can now change his userlevel just by sending a POST Request with "userlevel=>100".

So if you don't check if the key are allowed in your request array you'll get serious sql injection vulnarabilities in your code.
up
-2
jhooks
18 years ago
> Return Values
>
> Returns TRUE on success or FALSE on failure. Returns string if
> PGSQL_DML_STRING is passed via options.

I have found in my copy of PHP (version 4.4.0) that if you use the 'PGSQL_DML_STRING' option, the function does not execute any query. It merely returns the query which would have been executed.

Another thing I noticed, pg_update does not seem to make use of pg_trace (atleast in 4.4.0).

PS this isn't a bug report, just an explanation of some undocumented features I noticed. As the manual says, the function is still in development so this behaviour may differ from version to version.
up
-5
sdibb at myway dot com
19 years ago
This function is similar to PEAR::DB's autoExecute() function, with the only difference being that the where clause is an array instead of a string.

Also, if you want to use your instance of the DB class with this function, you can reference the existing resource connection with $db->connection.

An example would be:
<?
pg_update
($db->connection, $arr_update, $arr_where);
?>
To Top