我使用活动记录来更新我的数据库中的数据及其在数据库中的更新,但是在更新数据库内的数据后,我希望它重定向回我的编辑页面,显示来自我的数据库的更新数据..但是我试过不行。
我的codeigniter版本是2.7
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class account extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->login_model->database($username, $password);
if ($result == TRUE){
$data['posts'] = $result;
$data['subview'] = 'testview';
$this->load->view('display', $data);
}else{
echo "invalid password";
};
}
public function edit(){
$id = $this->input->post('id');
$name = $this->input->post('name');
$email = $this->input->post('email');
$telephone = $this->input->po
<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
ST(&#39;电话&#39); $ result = $ this-&gt; login_model-&gt; update($ id,$ name,$ email,$ telephone);
$data['posts'] = $result;
$data['message'] = "Record Updated Successfully."; // Print this Message in your edit view page as echo $message.
$data['subview'] = 'testview';
$this->load->view('display', $data);
}
}
<!--THIS IS MY MODEL-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function database($username, $password)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() ==1){
return $query->result();
}else{
return false;
}
}
public function update($id){
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'telephone' => $this->input->post('telephone'),
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}
}
&#13;
答案 0 :(得分:0)
Please update your model's update fuction like the following code.
public function update()
{
$id = $this->input->post('id');
$name = $this->input->post('name');
$email = $this->input->post('email');
$telephone = $this->input->post('telephone');
$data = array(
'name' => $name,
'email' => $email,
'telephone' => $telephone,
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}
Create a new function in your model for fetch the record with same updated id like the following
public function view($id)
{
$this->db->where('id', $id);
$result = $this->db->get('user');
return $result->result();
}
Please update your controller's edit Function like the following
public function edit(){
$id = $this->input->post('id');
$result = $this->login_model->update();
if ($result)
{
$resultData = $htis->login_model->view($id);//Calling the newly create model function for tething the record.
$data_arr['id'] = $id;
$data_arr['name'] = $resultData[0]->name;
$data_arr['email'] = $resultData[0]->email;
$data_arr['telephone'] = $resultData[0]->telephone;
$this->load->view('edit', $data_arr);//Loading the edit view
}
}
Assume, your edit view is as follows, just I am creating a view with neccessary fiels only
View Name: edit.php //Saved in to view folder.
<form method="post" action="<?php echo site_url('account/edit');?>"> <!--account is the controller and edit is the controller's function-->
<input type="text" name="id" value="<?php if(isset($id)){ php echo $id;}?>">
<input type="text" name="name" value="<?php if(isset($name)){ php echo $name;}?>">
<input type="text" name="email" value="<?php if(isset($email)){ php echo $email;}?>">
<input type="text" name="telephone" value="<?php if(isset($telephone)){ php echo $telephone;}?>">
<input type="submit" value="Edit" name="submit">
</form>