php 如何在codeigniter中更新表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27356776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to update form data in codeigniter
提问by pramod24
hey guys i am new in codeigniter. I am using codeigniter for this project. i have not getting how to update form data in the database.i have inserting ,showing data in the databse is done . but i cant understand how to update data in the database.plz
嘿伙计们,我是 codeigniter 的新手。我正在为这个项目使用 codeigniter。我不知道如何更新数据库中的表单数据。我已经插入,在数据库中显示数据已完成。但我无法理解如何更新 database.plz 中的数据
my controller:
我的控制器:
class User extends CI_Controller {
public function __construct() {
// Call the Model constructor
parent::__construct();
$this->load->model('usermodel');
}
public function insert() {
$this->load->view('userview');
if ($this->input->post('submit')) {
$this->usermodel->save();
}
}
public function display() {
$data = array();
$data['result'] = $this->usermodel->get_contents();
$this->load->view('usergrid', $data);
}
public function edit() {
$data = array();
$get = $this->uri->uri_to_assoc();
$data['result'] = $this->usermodel->entry_update( $get['id'] );
$this->load->view('useredit', $data);
if ($this->input->post('submit')) {
$this->usermodel->entry_update1($get['id']);
}
}
}
model:
模型:
<?php
class Usermodel extends CI_Model {
public function __construct() {
// Call the Model constructor
parent::__construct();
}
public function save() {
//print_r($this->input->post('name'));
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address')
);
//var_dump($this->db);
$this->db->insert('user', $data);
}
public function get_contents() {
$this->db->select('*');
$this->db->from('user');
$query = $this->db->get();
return $result = $query->result();
}
public function entry_update( $id ) {
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$id );
$query = $this->db->get();
return $result = $query->row_array();
}
public function entry_update1($id) {
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
'address' => $this->input->post('address')
);
$this->db->where('id', $id);
$this->db->update('user', $data);
}
}
?>
view:
看法:
<html>
<head>
<title>user registration</title>
</head>
<body>
<form action="edit" method="POST" name="myform">
<input type="hidden" name="id" value="<?php echo $result['id']; ?>">
username :<input type="text" name="name" value="<?php echo $result['name'] ?>"></br>
age :<input type="text" name="age" value="<?php echo $result['age'] ?>"></br>
Address :<input type="text" name="address" value="<?php echo $result['address'] ?>"></br>
<input type="submit" value="update" name="submit">
</form>
</body>
</html>
Thank you in advance for your help.
预先感谢您的帮助。
采纳答案by Rana Soyab
You are only passing $id in
你只是在传递 $id
$this->usermodel->entry_update1($get['id']);
$this->usermodel->entry_update1($get['id']);
and in function u did
在功能上你做了
public function entry_update1($id) {
$this->db->where('id', $id);
$this->db->update('user', $data);
}
so you have to pass $data also in you function call
所以你必须在你的函数调用中传递 $data
$this->usermodel->entry_update1($get['id'], $data);
$this->usermodel->entry_update1($get['id'], $data);
public function entry_update1($id, $data) {
$this->db->where('id', $id);
$this->db->update('user', $data);
}
回答by Craig
Just by having a quick look, I can see you're not passing anything to the $data in your entry_update1 function;
快速浏览一下,我可以看到您没有向 entry_update1 函数中的 $data 传递任何内容;
public function entry_update1($id) {
$this->db->where('id', $id);
$this->db->update('user', $data);
}
You're trying to update 'user' with $data, but you haven't set $data.
您正在尝试使用 $data 更新“用户”,但您尚未设置 $data。
回答by Nirav
You have to run update query this way in CodeIgniter.
您必须在 CodeIgniter 中以这种方式运行更新查询。
public function entry_update1($id,$data) {
$this->db->set($data);
$this->db->where('id',$id);
$update = $this->db->update('user');
if($update)
{
return true;
}
else
{
return false;
}
}