2014-03-02 158 views
0

我試圖更新MySQL數據庫具有以下功能,創建減去$席位變量從現有值的值在地方領域的更新語句的值在數據庫中。 下面的代碼返回0,而不是通過$ seats變量的值減少位置值。更新MySQL數據庫的PHP語句

$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. 

}

與糾正我的語法任何幫助是極大的讚賞。

+0

我認爲你必須改變'$ update_seats ='\'places \'' - $ seats;'by:'$ update_seats ='\'places \' - '。 $ seats;' – Seb33300

+0

什麼是您的$ modx變量?它也看起來像你還沒有定義$座位。 –

+0

嗨Seb33300 - 感謝您的幫助。 我嘗試了以下錯誤結果消息的建議: 'SQL> UPDATE'database_name'.'table_name' SET'places' ='places' - ($ seats value should be here)WHERE'id' =「1104」 AND'places'> 0' $ seats變量的值未被髮送。 它在功能之外的頁面被進一步聲明。 – Twobears

回答

0

$seats變量內部函數未定義,你需要使用:

function 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); 
+0

嗨Lashane - 完美的工作,非常感謝您的幫助 – Twobears