2017-02-09 92 views
1

我有一个插入语句与表单数据,我必须给用户一个消息,如果它是成功与否。在哪里检查INSERT语句的成功或失败?

这是我的代码:

DB::get()->beginTransaction(); 
$this->inTransaction = true; 

$queryInsert = "INSERT INTO DB::$buchungenTabelle 
        (kurs_id, anrede, vorname, nachname, email, 
        telefon, status, anzahl_teilnehmer) 
       VALUES (?,?,?,?,?,?,?,?) 
       "; 

$stmt = DB::get()->prepare($queryInsert); 

$stmt->execute(array($eventId, $salutation, $firstName, $familyName, 
        $email, $telephone, 'gebucht', $participant)); 

DB::get()->commit(); 
$this->inTransaction = false; 

我应该检查​​或commit()的返回值? 即使在开放交易中,​​的返回值是否可靠?谢谢你的帮助!

回答

1

只要给予成功消息无条件。在插入失败的情况下,原因可能是服务器故障,并且在这种情况下,应该通过与您的特定应用程序代码完全独立的专用错误处理程序来显示通用错误消息“服务器错误,请稍后尝试”。

此外,根本不需要交易。因此您的代码将是

$queryInsert = "INSERT INTO DB::$buchungenTabelle 
       (kurs_id, anrede, vorname, nachname, email, 
       telefon, status, anzahl_teilnehmer) 
      VALUES (?,?,?,?,?,?,?,?)"; 
DB::get()->prepare($queryInsert)->execute(array($eventId, $salutation, $firstName, 
      $familyName, $email, $telephone, 'gebucht', $participant)); 
echo "Success"; 
+0

感谢您的回答!我想做一个事务,因为这些事件有最大数量的参与者,我想添加一个数据验证。你认为这种情况下的交易是否也是超级? – Vizz85

+0

对不起,我不明白。你想添加什么样的验证? –

+0

我的意思是验证发布的数据并检查是否有足够的可用空间来放置这个事件(在这里我需要另一个查询) – Vizz85