如何避免并记录数据库的一些意外情况?
例如,用户创建一个产品,并将其分配到一个类别中。
在类别中,我们有一些记录:
-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.
但是完成步骤 1 后是否有机会检查,并在 2b 开始之前。管理员删除手机类别。如果发生这种情况,数据库将插入一条没有 cat id watch 的记录。数据库会有这样的内容:
*-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
如果有机会这样做,我该如何避免这种情况?谢谢。
(使用 php 和 mysql 与 codeigniter)
For example, a user create a product, and assign it into a category.
In the category, we have some record:
-phone, id:0
-computer, id:1
-pad, id:2
and a user create a product, the product need to assign a category, and a user do something like this:
name:iphone
cat:0
And the flow will work like this:
1. check the cat is exist in db or not
2a. if not exist prompt error
2b. if exist, put the record in db.
But is there any chance when finished the step 1 checking, and before the 2b is start. The admin delete the phone category. If this happen, the database will insert a record which have no cat id watch. The db will have something like this:
*-phone, id:0* (not any more, because the admin delete it)
-computer, id:1
-pad, id:2
But there is a record like this:
name:iphone //but it belong to no category, because the admin delete it.
cat:0
If there is a chance for doing this, how can I avoid this? Thank you.
(Using php and mysql with codeigniter)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该考虑两件事:
There are two things you should think about:
将其放入 try..catch 块中,并使 MySQL 中的某些记录值不为空(必填字段),您将能够捕获异常并显示其中的错误。
Put it in a try.. catch block and make some of the record values in MySQL as not null(the fields which are required), you would be able to catch the exception and show the error from that.