Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免和记录数据库中的意外事件?_Php_Mysql_Database_Codeigniter_Consistency - Fatal编程技术网

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,现在已插入。我认为你没有完全理解这个问题