[英]update table row and save in database in php codeigniter
我正在从数据库中创建表,并希望向每行更新添加“编辑/删除”按钮,或从数据库中删除特定行。 我已经成功添加了工作的“删除”按钮,但是我不知道如何更新视图中表<td>
中的数据并将其发送到控制器。
这是我的代码:
查看文件
<table class="table table-striped">
<tr>
<td>name</td>
<td>age</td>
<td>gender</td>
<td>class</td>
<td>roll no.</td>
</tr>
<?php foreach($record as $r): ?>
<tr>
<td><?php echo $r->name; ?></td>
<td><?php echo $r->age; ?></td>
<td><?php echo $r->gender; ?></td>
<td><?php echo $r->class; ?></td>
<td><?php echo $r->roll no; ?></td>
<td><a href="" >Edit</a>
<a href="<?php echo base_url()."student/deleteRow" ?id="$r->name">"
onclick="return confirm
('Are you sure to Delete?')"><i class="icon-trash"></a></td>
</tr>
<?php endforeach; ?>
</table>
控制器功能
public function deleteRow(){
if(isset($_GET['id'])){
$id=$this->input->get('id');
$this->student_model->rowDelete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
我不知道现在如何在不影响以前的视图的情况下插入输入字段来更新表行。 任何建议都会有所帮助。
要编辑Studen数据,您需要将id或唯一列名称传递给数据库以获取该学生数据。
首先在<a href="">
标记中设置学生ID。
<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a>
然后,当您单击编辑时,它将带您到控制器。 您可以直接输入第三个url参数,如控制代码中所示:您也可以使用get as shon
您的控制器应为:
public function edit_student($id){
$student_data = $this->student_model->get_student_data($id);
$this->load->view('your_view',['student_data'=>$student_data)]);
}
这是您的模型,该模型从controllr获取ID,并找到学生数据并将密码传递回控制器:您的模型应为:
public function get_student_data($id){
$this->db->select('*');
$this->db->from('your_table_name');
$this->db->where('id',$id);
$query = $this->db->get();
$student_data = $query->$row_array();
if(isset($student_data) && !empty($student_data)){
return student_data;
} else {
return FALSE;
}
}
表单控制器将数据传递给视图。 在视图侧:
<?php
// Just to ensure the data. Comment it after testing
echo "<pre>";
print_r($student_data);
echo "</pre>";
?>
<form action="<?= base_url('student/update_student') ?>/<?= $student_data['id'] ?>">
<input name="your_column_name" value="<?= $student_data['your_column_name'] ?>">
// Try to do the same for all you column.
<input type="submit" value="updata">
</form>
这是用于更新数据的控制器
public function update_student($id){
$student_data = $this->input->post();
$status = $this->student_model->update_student($id,$student_data);
if($status == TRUE){
$this->load->view('your_view');
// Your Success view
} else {
// Your view if fail to update
$this->load->view('your_view');
}
}
这是更新数据的模型
public function get_student_data($id,$student_data){
$this->db->where('id',$id);
$this->db->update('your_table_name',$student_data);
if($this->db->affected_rows() == 1){
return TRUE;
} else {
return FALSE;
}
}
与删除操作非常相似。 像这样:
<td>
<a href="" >Edit/Delete</a>
<a href="<?php echo base_url('student/updateRow?id='.$r->name); ?><i class="icon-edit"></a><!-- This should be another method in Student controller -->
<a href="<?php echo base_url('student/deleteRow?id='.$r->name); ?>" onclick="return confirm('Are you sure to Delete?')"><i class="icon-trash"></a><!-- I changed order of edit and delete -->
</td>
我需要警告您使用CSRF。 如果您在此处未实现更好的安全性,则指向该链接的任何人都可以编辑或删除数据。 检查文档中的Security类以及如何设置隐藏值,以确保您只有已请求该页面的人才能编辑/删除行。
<td><a href="<?php echo base_url();?>controller_name/function_name/<?php echo $edit_id;?>" >Edit</a></td>
another way
<td><a href="<?php echo base_url();?>controller_name/function_name?id=<?php echo $edit_id;?>" >Edit</a></td>
您可以根据需要使用任何一种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.