php Codeigniter从数据库中删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23091163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter delete row from db
提问by NiceTry
Just want to delete row from db table by passing id. Please help me to pass id from the view correctly. Any help will be highly appreciated. Please let me know if any detailed information is needed so that I can provide it.
只想通过传递 id 从数据库表中删除行。请帮助我正确地从视图中传递 id。任何帮助将不胜感激。如果需要任何详细信息,请告诉我,以便我提供。
Here is my Model:
这是我的模型:
public function did_delete_row($user_id){
$query = $this->db->get_where('users',array('user_id' => $user_id));
if ($query->num_rows() == 1) {
if ($this->db->delete('users',array('user_id','email','name','city')))
{return true;}
else
{return false;}
} else {return false;}
}
Here is My view:
这是我的观点:
<a href="<?php echo site_url('admin/delete_row/'.$user_id);?">Delete</a>
Here is My controller:
这是我的控制器:
public function delete_row($user_id) {
$this->load->model("model_admin");
$this->model_admin->did_delete_row($user_id);
}
回答by Fisherman
In Your HTML.... you should pass the user_id_to_delete
like this:
在你的HTML.... 你应该通过user_id_to_delete
这样的:
<a href="<?php echo site_url('admin/delete_row/'.$user_id_to_delete);?>">Delete</a>
Now, in the Controller
you should receive the user_id_to_delete
variable into the user_id
when it gets to the delete_row()
method. Now, you can send it as a HTTP post transaction.
现在,当它到达方法时,Controller
您应该将user_id_to_delete
变量接收到。现在,您可以将其作为 HTTP 后事务发送。user_id
delete_row()
public function delete_row($user_id) {
$this->load->model("model_admin");
$this->model_admin->did_delete_row($user_id);
}
For the model you will get the user_id
to delete. Here is the function for your admin model
对于模型,您将得到user_id
删除。这是您的管理模型的功能
public function did_delete_row($id){
$this -> db -> where('user_id', $id);
$this -> db -> delete('users');
}
Finally, update the delete URL line as follows:
最后,更新删除 URL 行,如下所示:
<td><a href="<?php echo site_url('admin/delete_row/'.$user['user_id']);?">Delete</a></td>
That Should work for you
这应该适合你
回答by Stnfordly
Codeigniter 3:
代码点火器 3:
Use - $this->db->delete()
使用 - $this->db->delete()
$this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id
OR
或者
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);
https://www.codeigniter.com/userguide3/database/query_builder.html
https://www.codeigniter.com/userguide3/database/query_builder.html