使用php语句更新MySQL数据库
我试图用下面的函数更新MySQL数据库中的一个值,该函数创建一个update语句,该语句从数据库中places字段中的现有值减去$seats变量的值。 下面的代码返回0,而不是将places值减少$seats变量的值使用php语句更新MySQL数据库,php,mysql,modx,Php,Mysql,Modx,我试图用下面的函数更新MySQL数据库中的一个值,该函数创建一个update语句,该语句从数据库中places字段中的现有值减去$seats变量的值。 下面的代码返回0,而不是将places值减少$seats变量的值 $update_seats = update_places($id); function update_places($id) { global $modx; $table = "`database_name`.`table_n
$update_seats = update_places($id);
function update_places($id) {
global $modx;
$table = "`database_name`.`table_name`";
$update_seats = '`places`' - $seats;
$result = $modx->db->update( '`places` = ' . $update_seats, $table, '`id` = "' . $id . '" AND `places` > 0' );
return $result; // Returns 'true' on success, 'false' on failure.
}
非常感谢您对更正语法的任何帮助。
$seats
函数中未定义变量,您需要使用:
功能更新\u位置($id,$seats)
然后称之为$update\u seats=update\u places($id,123)代码>
另外,将查询更改为更正确:
$result = $modx->db->update( '`places` = ' . $update_seats, $table, '`id` = ' . intval($id, 10) . ' AND `places` >= ' . $seats );
我认为您必须更改$update\u seats=''places`-$seats代码>作者:$update\u seats=''places`-'$席位代码>您的$modx变量是什么?看起来您还没有定义$seats.Hi SEB333300-感谢您的帮助。我尝试了您的建议,并显示了以下错误结果消息:SQL>UPDATE
database\u name
table\u name`SETplaces
=places
-($seats值应在此处)其中id
=“1104”和places
>0`未发送$seats变量的值。它在功能之外的页面上进一步声明。