PDO值增量PHP,Mysql

时间:2012-09-01 13:55:34

标签: php mysql pdo set

大家好,我正致力于一个处理事件点的项目。我试图在有人注册之后更新事件点的值。有人注册或从数据库中读取值没有问题,但我无法更新这些点。希望你能帮忙..

以下是代码中不起作用的部分:

$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行中的值。

3 个答案:

答案 0 :(得分:3)

除了缺少的'

您可以直接在查询中执行更改:

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);

答案 1 :(得分:0)

没有引号     $ update_event_query_do - > bindParam(':full_spots,$ full_spots_new,PDO :: PARAM_INT);

试一试:

$update_event_query_do -> bindParam(':full_spots', $full_spots_new, PDO::PARAM_INT);

答案 2 :(得分:0)

您没有在参数中绑定:event_id ...