Vizz85
Vizz85

Reputation: 197

where to check success or failure of an INSERT statement?

I have an INSERT statement with form data and I have to give back a message to the user if it was successful or not.

This is my code:

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;

Should I check the return value of execute() or of commit()? Is the return value of execute() reliable even if it is in a open transaction? Thanks for your help!

Upvotes: 1

Views: 554

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157872

Just give a success message unconditionally. In case of a failed insert the reason is likely the server failure and in such a case a generalized error message "Server error. Please try later" should be shown by a dedicated error handler that is totally independent from your particular application code.

Besides, there is no need for a transaction at all. therefore your code would be

$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";

Upvotes: 1

Related Questions