pg_update
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
pg_update — 更新表
说明
PgSql\Connection
$connection
,string
$table_name
,array
$values
,array
$conditions
,int
$flags
= PGSQL_DML_EXEC
): string|bool
pg_update() 更新匹配 conditions
和 values
的记录。
如果指定 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_NULL
、PGSQL_DML_NO_CONV
、PGSQL_DML_ESCAPE
、PGSQL_DML_EXEC
、PGSQL_DML_ASYNC
或PGSQL_DML_STRING
的组合。当PGSQL_DML_STRING
是flags
的一部分,然后将返回查询字符串。当设置了PGSQL_DML_NO_CONV
或PGSQL_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";
}
?>
用户贡献的备注 3 notes
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.
> 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.
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);
?>