2017-08-05 52 views
0

我正在从数据库中创建一个表,并且想要为每一行添加一个编辑/删除按钮来更新或从数据库中删除一个特定的行。我已经成功添加了工作“删除”按钮,但我不知道如何更新表<td>中的数据并将其发送给控制器。更新表格行并保存在数据库中的php codeigniter

这里是我的代码:

视图文件

<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']); 
    } 
} 

我不知道我怎么能现在插入输入字段更新表行而不影响以前的观点。任何建议都会有所帮助。

+0

看到这个[menual](HTTPS:/ /www.formget.com/insert-data-into-database-using-codeigniter/)。 – Virb

回答

0

与删除所做的操作非常相似。就像这样:

<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 class以及如何设置隐藏值,以便确保只有已请求该页面的用户能够编辑/删除行。

0
<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> 

您可以根据您的要求使用任何一个。

0

要编辑Studen数据,您需要将一个id或uniue列名传递给数据库以获取该学生数据。

首先在<a href="">标记中设置学号。

<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a> 

然后当你点击编辑它会带你到控制器。你可以得到第三个URL参数直接作为节目的CONTROLER代码: 您还可以使用得到尽可能SHON

控制器应该是:

public function edit_student($id){ 

$student_data = $this->student_model->get_student_data($id); 

$this->load->view('your_view',['student_data'=>$student_data)]); 

} 

这里是你的模型当中去ID表单controllr并找到学生数据和passit备份控制器: 你的模式应该是:

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; 
    } 

} 
相关问题