I m trying use "$this->input->post();" of Codeigniter to do not need specify each field in my form. But i
m插入数据库时遇到问题。
看我的控制器:
public function cadastrar(){
$var = $this->input->post(null, TRUE);
$this->load->model('m_clientes');
$this->m_clientes->inserir($var);
}
我的控制器是简化在这里,因为我知道如何处理数据库在编码器。这篇文章的结果是:
Array ( [nome] => Raphael [sobrenome] => Schubert [cpf] => 893.528.432-89 [rg] => 4529875231908472 [telefone] => (53) 2980-5792 [celular] => (53) 9 2180-7529 [rua] => Israel de Almeida [numero] => 859 [cep] => 88.312-000 [bairro] => São Vicente [cidade] => ITAJAÍ [estado] => Santa Catarina [email] => rfswdp@gmail.com [tipo] => pf [cnpj] => 34.827.481/2834-78 [inscricaoestadual] => 34120489032814930128 [razaosocial] => Teste [nomefantasia] => Disney [dataaberturaempresa] => 10/21/15 [proprietario] => Marcos Aurelio )
我通常用这种方式插入:
$data = array(
'user_name' => $this->input->post('user_name',TRUE);
'user_phone' => $this->input->post('user_phone',TRUE);
'user_role' => $this->input->post('user_role',TRUE);
);
$this->name_of_model->inserir($data);
和工作……
但我试图使用只是$this->input->post();从表单中获取所有字段。因为我的实际应用程序将有数百个字段,我试图不写每一行。
所以我的模型实际上是: public function inserir($var){
if($var!=NULL):
print_r($var);
$this->db->insert('tb_usuarios',$var);
endif;
}
但我得到和错误说:
Message: Undefined property: clients::$db和消息:在null
上调用成员函数insert()我的表名是:"tb_usuarios"我改变了数据库中的所有字段接受NULL,看看我是否得到一些字段名称错误…但不工作…
提示吗? ?
不需要捕获$var
中的POST变量。您可以很好地看到模型中的POST变量。所以你需要在控制器中做的就是:
public function cadastrar(){
$this->load->model('m_clientes');
$this->m_clientes->inserir();
}
,在你的模型中:
public function inserir(){
if( count($this->input->post()) > 0):
$this->db->insert('tb_usuarios',$this->input->post());
endif;
}
表单中的字段名必须与表中的列名对应。消息消息:在null上调用成员函数insert()意味着您忘记加载数据库库,就像remiheens所说的那样。
但是我的建议是,对您的字段使用表单验证,这样您就可以确保所有必要的字段都使用每个字段所需的数据格式完成。虽然这可能需要大量的编码,但没有其他安全的方法可以避免数据库操作中的错误。您不能相信用户会正确插入数据,这就是需要表单验证的原因。
这里的$var = $this->input->post(null, TRUE);
使用null
。null
不是有效的输入名。name = ''
,这将工作$this->input->post('user_name',TRUE);
它的cz有input
标签name
(name = 'user_name'
)
我们使用
,TRUE)
旁边的输入post字段来允许XSS保护
$var = $this->input->post(null, TRUE);
是错误的
当你尝试这个时,它显示
Message: Undefined property: clients::$db and Message: Call to a member function insert() on null
不能工作
public function cadastrar(){
$var = $this->input->post(null, TRUE);//will never give valid response
$this->load->model('m_clientes');
$this->m_clientes->inserir($var);
}
适用
public function cadastrar(){
$this->load->model('m_clientes');
$data = array(
'user_name' => $this->input->post('user_name',TRUE);
'user_phone' => $this->input->post('user_phone',TRUE);
'user_role' => $this->input->post('user_role',TRUE);
);
$this->name_of_model->inserir($data);
$this->load->model('m_clientes');
}
这是因为您的数据库没有加载到codeigniter实例中。($ this -> db)只需尝试自动加载"数据库"库(config/autolload .php)或加载/连接你的数据库在你的模型:
$ this ->加载->数据库();
不要忘记编辑你的config/database.php;)
我每次处理数百个字段,但我也基本上验证了每个字段。我总是这样做:
$customer['name'] = $this->input->post('customer_name');
$customer['age'] = $this->input->post('customer_age');
$customer['country'] = $this->input->post('customer_country');
$customer['city'] = $this->input->post('customer_city');
// validations
if(isAgeValid($customer['age']) == FALSE)
{
echo 'Hold on, your age...hmm check it out!';
return;
}
$this->customers_model->add($customer);
处理插入的函数只有:
public function add($data)
{
$data = $this->security->xss_clean($data);
$this->db->insert('customers', $data);
return $this->db->insert_id();
}
非常干净和简单。现在,如果您不想验证字段,或者只想验证其中的一些字段,并希望插入其他字段而不进行验证,这就是我基于前面代码的目的:
// validations..
if(isAgeValid());
$customer = array();
foreach($_POST as $key => $value)
$customer[$key] = $value;
$this->customers_model->add($customer);
$data = array(
'user_name' => $this->input->post('user_name');
'user_phone' => $this->input->post('user_phone');
'user_role' => $this->input->post('user_role');
);
$this->load->model('name_of_model');
$this->name_of_model->inserir($data);
Model:
public function inserir($data)
{
$this->db->insert('***', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
}