简体   繁体   English

如果我想删除数据库中的数据并且它是codeigniter中的外键,如何进行限制?

[英]how can I make restrictions if I want to delete a data in the database and it is foreign key in codeigniter?

I have two tables named "location" which consist of "location_id" and "location_name" and the other table is "computer" which have the "location" as a foreign key. 我有两个名为"location"表,由"location_id""location_name" ,另一个表是"computer" ,其中的"location"为外键。 Now whenever I want to delete a data which is used in the table "computer" I got an error message: 现在,每当我要删除表"computer"使用的数据时,都会收到错误消息:

A Database Error Occurred Error Number: 1451 发生数据库错误,错误号:1451

Cannot delete or update a parent row: a foreign key constraint fails ( cmms . computer , CONSTRAINT computer_ibfk_1 FOREIGN KEY ( location ) REFERENCES location ( location_id )) 无法删除或更新父行,外键约束失败( cmmscomputer ,约束computer_ibfk_1外键( location )参考locationlocation_id ))

DELETE FROM location WHERE location_id = '30' location WHERE location_id ='30'删除

Filename: C:/xampp/htdocs/cmms/application/models/asset_model.php 文件名:C:/xampp/htdocs/cmms/application/models/asset_model.php

Line Number: 9 行号:9

How can I check if it exist in table "computer" there will be a message and if it doesn't exist it will automatically delete the data in "location" table? 如何检查“计算机”表中是否存在该消息,如果不存在,它将自动删除“位置”表中的数据?

my code in my controller which is asset_management.php is: 我在控制器中的代码为asset_management.php

public function deleteloc(){
  $this->load->model("asset_model");
  $u = $this->uri->segment(3);
  $this->asset_model->deletelocation($u);
  redirect('asset_management/locations');         
}

My code in my model which is asset_model is: 我在模型中的代码为asset_model:

public function deletelocation($a){
    $this->db->delete('location', array('location_id' => $a));
    return;
}

please help. 请帮忙。 THANK YOU SO MUCH!! 非常感谢!!

You need to remove a value from the computer table before being able to remove this row. 您需要先从计算机表中删除一个值,然后才能删除该行。 There is a row in the computer table depending on this row. 计算机表中有一行取决于该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用php pdo从具有外键约束的数据库中删除数据 - How can i delete data from database with foreign key constraint in mysql using php pdo 我想按条件删除数据。如果数据在另一个表中作为外键可用,则不要删除它 - I want to delete data on condition.if data is available in another table as a foreign key then donot delete this 如何在删除时处理 MySql + Laravel 中的外键约束? - How can I deal with foreign key constraints in MySql + Laravel on delete? 我想在我的自定义 PHP 项目中使用 codeigniter 外国字符库,我该如何使用它? - I want to use codeigniter foreign character library in my custom PHP project how i can use it? 如何从CodeIgniter中的数据库和Image文件夹中删除图像 - How I can delete image from database and Image folder in CodeIgniter 我想删除 xml 中的数据,但它不能 - I want to delete data in xml but it can't 如何访问数据库中的数据? -CodeIgniter - How can I access data in my database? - CodeIgniter 我如何使用 php 和 codeigniter 将数据插入数据库 - How can i Insert data to database using php and codeigniter 如何从与 codeigniter 中的用户关联的数据库中获取数据 - How can I fetch data from the database that is associated with the user in codeigniter 我无法将数据插入数据库(codeigniter) - I can not insert data into database (codeigniter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM