如何在Codeigniter中编辑和删除数据库中的用户

时间:2014-09-07 10:33:12

标签: php codeigniter admin

我不知道如何在codeigniter中执行编辑和删除功能..我真的需要它为我的系统的管理员..我真的需要帮助..我想删除我的表中的ID命名为user_acc并编辑整个列

型号:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_admin extends CI_Model{
 function __construct()
    {
        parent::__construct();
    } 

    public function user_acc(){
        $this->db->select('*');
        $this->db->from('user_acc');
        $query = $this->db->get();
        return $query->result();
    }


    public function delete()
    {
        $this->db->where('id');
        $this->db->delete();
    }



}

管理:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {


      public function __construct()

    {
        parent::__construct();

        $this->load->helper('url');
       $this->load->Model('Mod_admin');

    }

public function manageuser(){
    $this->load->view('template/adminhead');
    $data['user_acc'] = $this->Mod_admin->user_acc();
    $this->load->view('manageuser', $data);
}


public function delete_product() {
    $this->Mod_admin->delete();
    $this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');
    redirect('manageuser');
}



}

查看:

<html>

<title>Manage User</title>
<body>

<style>
  .shit{
    border: 1px solid;
      border-collapse: collapse;
  }

   .shit td{
    border: 1px solid;
      border-collapse: collapse;
      padding: 7px;
  }

  .shit td tr{
    border: 1px solid;
  }
  .shit td th{
    border: 1px solid;
  }
</style>
  <div id="container">
  <table class="shit">
        <tr>
            <th><strong>ID</strong></th>
            <th><strong>Firstname</strong></th>
            <th><strong>Lastname</strong></th>
            <th><strong>Username</strong></th>
            <th><strong>Password</strong></th>
            <th><strong>E-mail</strong></th>
            <th><strong>Contact</strong></th>
            <th><strong>Edit</strong></th>
            <th><strong>Delete</strong></th>
        </tr>

    <?php


                foreach($user_acc as $row){

                echo 

                      '<tr> 

                          <td>'.$row->id.'</td>
                          <td>'.$row->firstname.'</td>
                          <td>'.$row->lastname.'</td>
                          <td>'.$row->username.'</td>
                          <td>'.$row->password.'</td>
                          <td>'.$row->email.'</td>
                          <td>'.$row->contact.'</td>
                          <td>'.'<a href="#">'.'Edit</a>'.'</td>
                          <td>'.'<a href="#">'.'Delete</a>'.'</td> 
                      </tr>';



                }
            ?>
</table>
</div>



</body>
</html>]

How View looks like 请帮助我编辑和删除用户,谢谢

1 个答案:

答案 0 :(得分:0)

更新

在控制器中

$result = array( 'column1'=>$val1,'column2'=>$val2,'column3'=>$val3);
$this->model_name->updateEvent( $id , $result );

在模型中:

public function updateEvent($id, $result){
     $this->db->where('db_table_column', $id);
     $this->db->update('table_name', $result);
}

删除

在控制器中:

$this->model_name->deleteEvent( $id );

在模型中:

public function deleteEvent( $id )
{
    $this->db->where('db_table_column', $id);
    $this->db->delete('table_name');
}