我是CodeIgniter的新手。我正在为我的项目使用CodeIgniter。我已经完成了在数据库中插入数据并从数据库中检索数据的操作,现在我正试图从数据库中的表中删除行,但是我无法做到这一点。
我尝试了许多其他方式来做到这一点,但还是没有运气。我的代码如下。
控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class School extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper(array('url','language'));
$this->load->model('Main_model');
}
public function index()
{
if($this->input->post('submit'))
{
$data=array(
'name'=> $this->input->post('name'),
'email'=> $this->input->post('email'),
'phone'=> $this->input->post('phone'));
$insert=$this->Main_model->std($data);
}
$this->data["fetch_user"]=$this->Main_model->fetch_user();
$this->load->view('form',$this->data);
}
public function delete_data()
{
$id=$this->uri->segment(3);
$this->Main_model->delete_data($id);
redirect(base_url()."deleted");
}
public function deleted()
{
$this->index();
}
}
?>
型号:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main_model extends CI_Model
{
public function std($data)
{
$insert=$this->db->insert('user',$data);
return $this->db->insert_id();
}
function fetch_user()
{
$this->db->select('*');
$this->db->from('user');
$query=$this->db->get();
return $query->result();
}
function delete_data($id)
{
$this->db->where("id",$id);
$this->db->delete("user");
}
}
?>
视图:
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<style>
input[type=text],input[type=number],input[type=email]
{
height:30px;
width:250px;
outline:none;
}
input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button
{
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin: 0;
}
td,th,h3
{
font-size:18px;
font-family:arial;
}
input[type="submit"]
{
padding:10px 20px;
border:none;
border-top-left-radius: 25px;
border-bottom-right-radius: 25px;
background:#7e57c2;
color:#ffffff;
outline:none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h3>School Student data</h3>
<form method="post">
<table cellspacing="5" cellpadding="5">
<tr>
<td>Name</td>
<td><input type="text" name="name" required></td>
</tr>
<tr>
<td>Phone No.</td>
<td><input type="number" name="phone" required></td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Insert"></td>
</tr>
</table>
</form>
<br><br>
<table border="1" cellspacing="5" cellpadding="5" >
<tr>
<th style="padding:10px 20px;">ID</th>
<th style="padding:10px 20px;">Name</th>
<th style="padding:10px 20px;">Email</th>
<th style="padding:10px 20px;">Phone No.</th>
<th style="padding:10px 20px;">Action</th>
</tr>
<?php
if($fetch_user !=null)
{
foreach($fetch_user as $row)
{
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->phone;?></td>
<td><a href="#" class="delete_data" id="<?php echo $row->id;?>">Delete</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="4">sorry no data found</td>
</tr>
<?php
}
?>
</table>
<script>
$(document).ready(function(){
$('.delete_data').click(function(){
var id=$(this).attr("id");
if(confirm("are you sure you want to delete this?"))
{
window.location="<?php echo base_url(); ?>delete_data/"+id;
}
else
{
return false;
}
});
});
</script>
</body>
</html>
答案 0 :(得分:0)
您可以直接设置锚标记链接,而不是使用jQuery:
<td><a href="<?php echo base_url('delete_data').'$row->id'; ?>" id="<?php echo $row->id;?>">Delete</a></td>
这将直接将您发送到控制器的delete_data函数。 (而不是在jquery中使用class和click事件)
如果成功,则将自己从模型返回到控制器。
function delete_data($id)
{
$this->db->where("id",$id);
$this->db->delete("user");
return;//onsuccess
}