PHP codeigniter - 更新行后如何返回DB表

时间:2017-03-19 16:12:53

标签: php mysql database codeigniter

我希望我的管理表视图在使用后选择编辑,更改条目并选择更新条目并使用修改后的表格行中反映的编辑重新加载。

管理编辑页面工作正常,我知道将更新重定向到表页面的解决方法是基本的。一些光导将非常感谢!

管理员视图:

<html>
<head>
    <title>Admin Page</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>


<div class="container">
<table class="table table-bordered">
    <center>
    <thead> 
        <tr style="font-size: 24; color: white;">
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th colspan="2">Actions</th>
        </tr>
    </thead>
    </center>
    <tbody style="background-color: #F0F8FF;">  
        <?php 
        foreach($users as $u)
        {
            $class = "btn btn-primary";
            $button = "Approve";  
            ?>
        <tr style="font-size: 20;">
            <td><?php echo $u->first_name; ?></td>
            <td><?php echo $u->last_name; ?></td>
            <td><?php echo $u->email; ?></td>
            <td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
            <td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve/?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td> 
        </tr>
        <?php } ?>
    </tbody>
</table>
</div>


</body>
</html>

管理员编辑视图:

<html>
<head>
    <title>Edit User Info</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>

<div class="container">
    <div class="jumbotron">
        <form action="" method="post">


<p><font color="black" font size='5'/><strong>Entry for ID </strong><strong>"<?php echo $id; ?>"</strong></font></p>

<font color="black" font size='5'/><strong>First Name  </strong></font><input type="text" name='first_name' value="<?php echo $first_name; ?>"?><br/>

<font color="black" font size='5'/><strong>Last Name  </strong></font><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>

<font color="black" font size='5'/><strong>Email </strong></font><input type="text" name='email' value="<?php echo $email; ?>"/><br/>
<br/>
<input type="button" class="btn btn-info" value="Update Entry">

</div>
</div>
</form>
</body>
</html>

控制器:

<?php

class Myform extends CI_Controller{

  public function __construct(){
    parent::__construct(); 
  }

  public function index(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('user[first_name]', 'First Name', 'required'); 
    $this->form_validation->set_rules('user[last_name]', 'Last Name', 'required'); 
    $this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim'); 

    $form['userValues'] = [
        'first_name' => $this->input->post('user[first_name]'),
        'last_name'  => $this->input->post('user[last_name]'),
        'email'      => $this->input->post('user[email]'),
        'id'         => $this->input->post('id'),
    ];   

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $user = $this->input->post('user');
        //$company = $this->input->post('company'); 
        echo '<pre>';
        print_r($user);
        //print_r($company); 

        if ($this->input->post('id') !== '' && $this->input->post('id') !== NULL)
        {
            $this->db->where('id', (int) $this->input->post('id'));
            $this->db->update("User_Inputs", $user);
        }
        else
        {
            $this->db->insert("User_Inputs", $user);
        }
            //$this->db->insert("companies", $company);         
        redirect("/myform/successmessage");
    }
}

    public function successmessage(){
        $this->load->view('formsuccess');
    }          

  public function getDatabase(){
    $data['users'] = $this->db->get("User_Inputs")->result();
    $this->load->view('adminlogin', $data);

  }

public function approve(){
    $id = $this->input->get("user_id"); 
    $this->db->where("id", $id); 
  $data['user'] = $this->db->get("User_Inputs")->row();
    echo "<pre>"; 
  print_r($this->db->last_query()); 
    print_r($data);
    echo "<h1>". $data['user']->email."</h1>";  

    die();
}

public function edituser($user_id = NULL)
{
    $user_id = (int) $user_id;

    $data['user'] = $this->db
        ->where('id', $user_id)
        ->get("User_Inputs")
        ->row();

    $form['dbValue'] = [
        'first_name' => $data['user']->first_name,
        'last_name'  => $data['user']->last_name,
        'email'      => $data['user']->email,
        'id'         => $user_id
    ];

    $this->load->view('editfields', $data['user']);
  }
}

0 个答案:

没有答案