Php 如何避免和记录数据库中的意外事件?
例如,用户创建一个产品,并将其分配到一个类别中 在该类别中,我们有一些记录:Php 如何避免和记录数据库中的意外事件?,php,mysql,database,codeigniter,consistency,Php,Mysql,Database,Codeigniter,Consistency,例如,用户创建一个产品,并将其分配到一个类别中 在该类别中,我们有一些记录: -phone, id:0 -computer, id:1 -pad, id:2 用户创建一个产品,该产品需要分配一个类别,用户可以这样做: name:iphone cat:0 1. check the cat is exist in db or not 2a. if not exist prompt error 2b. if exist, put the record in db. *-phone, id:
-phone, id:0
-computer, id:1
-pad, id:2
用户创建一个产品,该产品需要分配一个类别,用户可以这样做:
name:iphone
cat:0
1. check the cat is exist in db or not
2a. if not exist prompt error
2b. if exist, put the record in db.
*-phone, id:0* (not any more, because the admin delete it)
-computer, id:1
-pad, id:2
name:iphone //but it belong to no category, because the admin delete it.
cat:0
流程的工作原理如下:
name:iphone
cat:0
1. check the cat is exist in db or not
2a. if not exist prompt error
2b. if exist, put the record in db.
*-phone, id:0* (not any more, because the admin delete it)
-computer, id:1
-pad, id:2
name:iphone //but it belong to no category, because the admin delete it.
cat:0
但是,当完成步骤1检查时,在2b开始之前,是否有任何机会。管理员删除电话类别。如果发生这种情况,数据库将插入一条没有cat id的记录。db将有如下内容:
name:iphone
cat:0
1. check the cat is exist in db or not
2a. if not exist prompt error
2b. if exist, put the record in db.
*-phone, id:0* (not any more, because the admin delete it)
-computer, id:1
-pad, id:2
name:iphone //but it belong to no category, because the admin delete it.
cat:0
但有这样一个记录:
name:iphone
cat:0
1. check the cat is exist in db or not
2a. if not exist prompt error
2b. if exist, put the record in db.
*-phone, id:0* (not any more, because the admin delete it)
-computer, id:1
-pad, id:2
name:iphone //but it belong to no category, because the admin delete it.
cat:0
如果有机会这样做,我该如何避免呢?多谢各位
(与codeigniter一起使用php和mysql)试试看。。catch块并将MySQL中的一些记录值设置为非空(必填字段),您将能够捕获异常并显示由此产生的错误。有两件事需要考虑:
这不起作用,在步骤1之后,脚本具有cat_id。然后脚本进入2b,但在执行2b之前,cat被删除。脚本仍然在变量中包含id,现在已插入。我认为你没有完全理解这个问题