PDO 值递增 PHP、Mysql [英] PDO Value Increment PHP, Mysql
问题描述
大家好,我正在研究一个处理事件点的项目.我正在尝试在有人注册后更新事件点的值.有人注册或从数据库读取值没有问题,但我无法更新这些点.希望能帮到你..
Hello guys im working on a project for handling spots on a event. I'm trying to update the values of the event spots after someone registers to it. There is no problem with someone registering or reading the values from database but i cant update the spots. Hope you can help..
这是代码的不工作部分:
Here is the not working part of the code :
$free_spots_new = $free_spots - 1 ; // i haven't written the code up there, $free_spots is the value of the free_spots value in the database. And this is the process after someone registers to this event ...
$full_spots_new = $full_spots + 1 ; // same in here
try{
$update_event_query = "UPDATE `events` SET `free_spots` = :free_spots, `full_spots`= :full_spots WHERE `event_id`=:event_id";
$update_event_query_do = $db->prepare($update_event_query);
$update_event_query_do -> bindParam(':free_spots', $free_spots_new, PDO::PARAM_INT);
$update_event_query_do -> bindParam(':full_spots', $full_spots_new, PDO::PARAM_INT);
$update_event_query_do ->execute() or die(print_r($update_event_query_do->errorInfo(), true));
}
catch(PDOException $e) {
$log->logError($e." - ".basename(__FILE__));
}
还可以在不定义新变量(如 $free_spots_new exc)的情况下更新 UPDATE 行中的值.
Also is it possible to update the values within the UPDATE line without defining a new variable like $free_spots_new exc.
推荐答案
Beside the missing '
Beside the missing '
您可以直接在查询中执行更改:
You can execute the alterations directly in the query:
UPDATE `events`
SET `free_spots` = `free_spots`-1,
`full_spots` = `full_spots`+1
WHERE `event_id` = :event_id
您还需要绑定 :event_id 占位符$update_event_query_do->bindParam(':event_id', $event_id, PDO::PARAM_INT);
Your also need to bind the :event_id placeholder
$update_event_query_do->bindParam(':event_id', $event_id, PDO::PARAM_INT);
这篇关于PDO 值递增 PHP、Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!