删除并更新代码点火器中的特定行


delete and update specific row in codeigniter

我是codeigniter的新手。在我的视图页面中,我将数据库中的数据显示在表中,我在表中有两个按钮来编辑和删除行。我想通过 id 从数据库中删除特定行。

## view


                    <thead>
                         <tr>
                              <th>#</th>
                              <th>First Name</th>
                              <th>last name</th>
                               <th>Email</th>
                         </tr>
                    </thead>
                    <tbody>
                         <?php for ($i = 0; $i < count($records); ++$i) { ?>
                              <tr>
                                   <td><?php echo ($i+1); ?></td>
                                   <td><?php echo $records[$i]->fname; ?></td>
                                    <td><?php echo $records[$i]->lname; ?></td>
                                    <td><?php echo $records[$i]->email; ?></td>
                                    <td><button name="edit">Edit</button></td>
                                    <td><button name="delete">Delete</button></td>
                              </tr>
                         <?php } ?>
                    </tbody>

----------
## Controller ##
class Welcome extends CI_Controller {

    public function index()
    {

        $this->load->view('welcome_message',$data);
    }
    public function register()
     {
        $this->load->view("register");
     }
    public function receive()
    {
        $fname= $this->input->post("fname");
        $this->load->model('Model');
        $this->Model->insertdata($fname);
        // echo $this->input->post("fname");
        // echo $this->input->post("lname");
        // echo $this->input->post("password");
        // echo $this->input->post("Email");
    }
    public function all_user(){
        $this->load->model('Model');
         $data['records'] =  $this->Model->get_all_users();
        $this->load->view("store_data",$data);
    }
    public function row_delete(){
        $this->load->model('Model');
   $this->mod1->row_delete($id);
   redirect($_SERVER['HTTP_REFERER']);
    }
}

----------
## Model ##
class Model extends CI_Model{
    public function insertdata($fname)
    {


 $data = array
        ('fname'=> $this->input->post("fname"),
        'lname' => $this->input->post("lname"),
        'email' => $this->input->post("email"),
        'password' => $this->input->post("password"),

        );

    $this->db->insert('register',$data);
    }
    public function get_all_users()
 {
    $query= $this->db->get('register'); 
        return $query->result();
 }
    public function delete_row()
{$this->db->where('id', $id);
$this->db->delete(''); 

    }
}

在你的代码中试试这个

//view
<tbody>             
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>                                 
<td><a href="<?php echo base_url().'welcome/row_delete?id='.$records[$i]->id;?>">Delete</a></td>                       <td><a href="">Edit</a></td>                                  
</tr>
<?php } ?>
</tbody>

确保将记录 ID 传递给视图,并在配置文件上配置base_url

//controller
public function row_delete(){
if(isset($_GET['id'])){
$id=$_GET['id'];
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);}
}
//model
public function delete_row()
{
$this->db->where('id', $id);
$this->db->delete('register');
}

参考链接 这里 和 这里

只需将参数传递给 where 函数和表名即可删除函数:

public function delete_row()
{
    // Set where
    $this->db->where('id', $id);
    // Run query
    return $this->db->delete('register');
}

干杯