Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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语句更新MySQL数据库_Php_Mysql_Modx - Fatal编程技术网

使用php语句更新MySQL数据库

使用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

我试图用下面的函数更新MySQL数据库中的一个值,该函数创建一个update语句,该语句从数据库中places字段中的现有值减去$seats变量的值。 下面的代码返回0,而不是将places值减少$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
函数中未定义变量,您需要使用:

功能更新\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`SET
places
=
places
-($seats值应在此处)其中
id
=“1104”和
places
>0`未发送$seats变量的值。它在功能之外的页面上进一步声明。