gpt4 book ai didi

php - 使用php语句更新MySQL数据库

转载 作者:行者123 更新时间:2023-11-30 00:36:33 25 4
gpt4 key购买 nike

我正在尝试使用以下函数更新 MySQL 数据库中的值,该函数创建一个更新语句,从 places< 中的现有值中减去 $seats 变量的值/strong> 数据库中的字段。下面的代码返回 0,而不是将地方值减去 $seats 变量的值。

    $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 变量在函数内部未定义,您需要使用:

函数 update_places($id, $seats)

然后调用它$update_seats = update_places($id, 123);

另外,更改您的查询以使其更加正确:

$result = $modx->db->update( '`places` = ' . $update_seats, $table, '`id` = ' . intval($id, 10) . ' AND `places` >= ' . $seats );

关于php - 使用php语句更新MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com