【问题标题】:How to edit and delete users from database in Codeigniter如何在 Codeigniter 中编辑和删除数据库中的用户
【发布时间】:2014-09-07 10:33:12
【问题描述】:

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

型号:

<?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 请帮我编辑和删除用户谢谢

【问题讨论】:

    标签: php codeigniter admin


    【解决方案1】:

    更新

    在控制器中

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

    【讨论】:

    • 我怎样才能看到它?
    • 为什么可以放在view 页面上,却要显示它?您正在使用MVC 模式。从view 页面submit 你的form/actioncontroller action。从controller 调用model function
    • 我的意思是在按钮中.. 我应该在这里放什么'.''.'Edit'.' 我是要使用 ahref 还是 onclick?
    • &lt;a href="&lt;?=base_url?&gt;index.php/controller/action?param=&lt;?=$var?&gt;"&gt;Click&lt;/a&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2010-09-23
    • 1970-01-01
    • 2013-12-17
    • 2021-01-18
    • 2020-02-18
    相关资源
    最近更新 更多