php 错误:您必须使用“set”方法来更新条目修复?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30379100/
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
Error: You must use the "set" method to update an entry fix?
提问by ddrossi93
I am using codeigniter as my PHP framework, and I keep getting this error when I submit my from to post to my database.
我使用 codeigniter 作为我的 PHP 框架,当我提交我的 from to post 到我的数据库时,我不断收到此错误。
You must use the "set" method to update an entry
I am not exactly sure what that means, from the other posts I have looked at, everyone says that the datamapper needs to have a value assigned to the object.
我不太确定这意味着什么,从我看过的其他帖子中,每个人都说数据映射器需要为对象分配一个值。
Being that I am new to all this, could someone give me a better explaniation.
由于我对这一切都很陌生,有人可以给我一个更好的解释。
Here is my code where it says I have the error:
这是我的代码,它说我有错误:
class Add_book extends CI_Model {
public function add_book($data){
$this->db->insert('ST_ITM', $data);
}
}
?>
Thank you.
谢谢你。
采纳答案by Mr. ED
You need to do something like this Example Only
你只需要做这样的事情 Example Only
class Add_book extends CI_Model {
public function add_book(){
// 'book_name' would be the name of your column in database table
$data = array(
'book_title' => $this->input->post('book_title'),
'book_author' => $this->input->post('book_author'),
'book_name' => $this->input->post('book_name')
);
$this->db->set($data);
$this->db->insert($this->db->dbprefix . 'ST_ITM');
}
}
On view the input would be like example
在视图中,输入将类似于示例
<input type="text" name="book_name" value="" />
<input type="text" name="book_name" value="" />
<input type="text" name="book_title" value="" />
<input type="text" name="book_title" value="" />
<input type="text" name="book_author" value="" />
<input type="text" name="book_author" value="" />
Best to use a data array in model. and then load model function in success part of form validation Library
最好在模型中使用数据数组。然后在表单验证库的成功部分加载模型函数
回答by qwertzman
To all who arrive here, you do NOT have to use set in your insert queries:
对于所有到达这里的人,您不必在插入查询中使用 set:
https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data
https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
The error is a result of misconstructed insert data (must be an array) or failing to write the correct Active Record query like for example not adding the table name before the insert array.
该错误是由错误构造的插入数据(必须是数组)或未能编写正确的 Active Record 查询导致的,例如未在插入数组之前添加表名。
But sir, when dowe use set?
但是先生,我们什么时候使用 set 呢?
When you need to bypass automatic escaping during updates or replacingsfor example:
例如,当您需要在更新或替换期间绕过自动转义时:
$this->db->set('last_login', 'NOW()', FALSE);
$this->db->update(DB_PREFIX .'user', array('login_attempts' => 0));
The third parameter disabled auto-escaping. This gives us the necessary flexibility when writing Active Record queries.
第三个参数禁用自动转义。这为我们在编写 Active Record 查询时提供了必要的灵活性。
回答by Ritz
For Example I want to insert data, so I have to do code like below -
例如我想插入数据,所以我必须做如下代码 -
My View File(Login_form.php)
我的视图文件(Login_form.php)
<label>Username:</label> <input type="text" name="username" value="">
<label>Password:</label> <input type="password" name="password" value="">
My Model File(Model_login.php)
我的模型文件(Model_login.php)
class Model_login extends CI_Model {
public function insert_entry()
{
$data= array(
'username'=>$this->input->post('username'), // here username and password are database fields.
'password'=>$this->input->post('password'),
);
$this->db->insert('login', $data); //here login is my database table's name
}
}
Controller File(Login.php)
控制器文件(Login.php)
class Login extends CI_Controller {
public function index()
{
$this->load->view('Login_form'); //to load view file
}
public function getLoginValues()
{
$this->load->model('Model_login'); //to load Model file
$data=$this->Model_login->insert_entry();//to load method of Model file
}
}
It works fine, check it.
它工作正常,检查它。
回答by Valentino Pereira
I got this error when i was passing wrong variableinto the insert statement.
当我将错误的变量传递到插入语句时出现此错误。
For example :
例如 :
function($a){
$this->db->insert($aa); <-- error !!
}
function($a){
$this->db->insert($aa); <-- error !!
}
So I solved it by passing in the right variable, which is $a.
所以我通过传入正确的变量 $a 来解决它。
Also make sure your insertion array is correctly formatted as $a = array('columnname'=>'value');
还要确保您的插入数组格式正确$a = array('columnname'=>'value');