Php 将数组传递给模型?
只是为了澄清所有的答案,这里的工作完美无瑕!我选择了埃德温,因为他是第一个发帖的Php 将数组传递给模型?,php,mysql,codeigniter,variable-assignment,Php,Mysql,Codeigniter,Variable Assignment,只是为了澄清所有的答案,这里的工作完美无瑕!我选择了埃德温,因为他是第一个发帖的 $this -> load -> model('staff_model'); $this -> load -> library('encrypt'); $arrData = array( 'anum' => $this -> encrypt -> encode($this->input-
$this -> load -> model('staff_model');
$this -> load -> library('encrypt');
$arrData = array(
'anum' => $this -> encrypt -> encode($this->input->post('anum')),
'first' => $this -> input -> post('fname'),
'last' => $this -> input -> post('lname'),
'why' => $this -> input -> post('why'),
'aidyear' => $this -> input -> post('aidyear'),
'signintime' => NULL,
'comments' => $this -> input -> post('comments'),
);
if ($insert = $this -> staff_model -> session($arrData)) {
redirect('staff_controller/signin_info');
} else {
$this -> studentlogin();
}
型号:
function session($arrData) {
$null = NULL;
$sql2 = "INSERT INTO session (anum, first, last, why, aidyear, signintime, studentcomments)
VALUES (?, ?, ?, ?, ?, ?, ?)";
$insert = $this -> db -> query($sql2, $arrData);
return $insert;
}
不能像这样定义数组变量。试着换成
$data = array (
'encrypted' => $this->encrypt->encode('anum'),
'first' => $this->input->post('first'),
'last' => $this->input->post('last'),
'aidyear' => $this->input->post('aidyear'),
'why' => $this->input->post('why'),
'comments' => $this->input->post('comments'),
);
不能像这样定义数组变量。试着换成
$data = array (
'encrypted' => $this->encrypt->encode('anum'),
'first' => $this->input->post('first'),
'last' => $this->input->post('last'),
'aidyear' => $this->input->post('aidyear'),
'why' => $this->input->post('why'),
'comments' => $this->input->post('comments'),
);
控制器
$this -> load -> model('staff_model');
$this -> load -> library('encrypt');
$this->load->library('form_validation');
$this->load->helper('url');
//validate form input
$this->form_validation->set_rules('anum', 'Anum', 'required');
other validation rules
.....
if ($this->form_validation->run() == FALSE)
{
redirect('back to previous page through controller');
}else{
$data = array (
'encrypted' => $this->encrypt->encode('anum'),
'first' => $this->input->post('first'),
'last' => $this->input->post('last'),
'aidyear' => $this->input->post('aidyear'),
'why' => $this->input->post('why'),
'comments' => $this->input->post('comments'),
);
if ($insert = $this -> staff_model -> session($data)) {
print_r($insert);
}
}
模型
控制器
$this -> load -> model('staff_model');
$this -> load -> library('encrypt');
$this->load->library('form_validation');
$this->load->helper('url');
//validate form input
$this->form_validation->set_rules('anum', 'Anum', 'required');
other validation rules
.....
if ($this->form_validation->run() == FALSE)
{
redirect('back to previous page through controller');
}else{
$data = array (
'encrypted' => $this->encrypt->encode('anum'),
'first' => $this->input->post('first'),
'last' => $this->input->post('last'),
'aidyear' => $this->input->post('aidyear'),
'why' => $this->input->post('why'),
'comments' => $this->input->post('comments'),
);
if ($insert = $this -> staff_model -> session($data)) {
print_r($insert);
}
}
模型
在控制器中使用此选项:
$arrData = array();
//在数组文件中使用与数据库表中相同的名称,如下所示
$arrData['anum'] = $this->encrypt->encode('anum');
$arrData['first'] = $this->input->post('first');
$arrData['last'] = $this->input->post('last');
$arrData['aidyear'] = $this->input->post('aidyear');
$arrData['why'] = $this->input->post('why');
$arrData['studentcomments'] = $this->input->post('comments');
if ($this -> staff_model -> session($arrData)) {
redirect('staff_controller/signin_info');
}
而在模型中只是使用
//不需要写变量名,因为我们已经在数组中使用了相同的字段名
function session($arrData)
{
if($this->db->insert($arrData))
return true;
else
return false;
}
在控制器中使用此选项:
$arrData = array();
//在数组文件中使用与数据库表中相同的名称,如下所示
$arrData['anum'] = $this->encrypt->encode('anum');
$arrData['first'] = $this->input->post('first');
$arrData['last'] = $this->input->post('last');
$arrData['aidyear'] = $this->input->post('aidyear');
$arrData['why'] = $this->input->post('why');
$arrData['studentcomments'] = $this->input->post('comments');
if ($this -> staff_model -> session($arrData)) {
redirect('staff_controller/signin_info');
}
而在模型中只是使用
//不需要写变量名,因为我们已经在数组中使用了相同的字段名
function session($arrData)
{
if($this->db->insert($arrData))
return true;
else
return false;
}
当然,您可以将数组传递给您的模型。但是你需要发布你的错误并展示你所尝试的。编辑了原始的帖子当然你可以将数组传递给你的模型。但是你需要发布你的错误并展示你所做的尝试。编辑了原始的帖子仍然会得到这样的信息:未定义变量:曾经发生过的变量错误插入会话anum,first,last,why,aidyear,signetime,studentcomments值NULL,NULL,NULL,NULL,NULL,NULL似乎没有传递任何内容,因为所有值都是空的NULL@RaGe10940不能使用$encrypted、$first等。。直接地因为它在数组中。更改$data['encrypted']、$data['first']等。仍然会收到消息:未定义变量:变量错误插入会话anum、first、last、why、aidyear、signintime、studentcomments值NULL、NULL、NULL、NULL、NULL似乎什么都没有传递,因为所有的值都是NULL@RaGe10940您不能使用$encrypted,美元优先等等。。直接地因为它在数组中。更改$data['encrypted']、$data['first']等。。