doudong8713 2014-05-17 11:00
浏览 25
已采纳

完成(db查询和邮件)或无

I am writing some code to deactivate a users account. When the form is submitted I update the database and email the user confirming the deactivation was successful.

However, if the email fails to send I do not want to update the database and if the query to update the database fails I do not want to send the email, it's all or nothing.

How should I do this? Try catches? Is it even possible? What do you recommend?

//DEACTIVATE ACCOUNT & LOG OUT ALL SESSIONS
$db->update("users", "id", "=", $user->data()->id, array(
    "active" => 0,
    "logout_all" => 1
));

//EMAIL USER
$mail->send(
    "user_deactivated", 
    array(
        "to" => $user->data()->email,
        "subject" => "Account Deactivated",
        "message" => null,
        "from" => "noreply@domain.com"
    ), 
    array(
        "name" => $user->data()->full_name
    )
);

//LOG USER OUT
$user->logout();

Thanks

  • 写回答

1条回答 默认 最新

  • du12197 2014-05-17 11:18
    关注

    You can try to use database transactions. Scheme of algoritm can looks:

    START TRANSACTION
    UPDATE
    IF NO ERRORS IN TRANSACTION THEN
        IF SEND EMAIL THEN
            COMMIT TRANSACTION
        ELSE
            ROLLBACK TRANSACTION
    ELSE
        ROLLBACK TRANSACTION
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键