我目前正在制作一个注册表格,您可以批量注册但我收到错误 错误号码:1054
未知栏' test@gmail.com'在'字段列表'
INSERT INTO user
(date_registered,test@gmail
。com
)VALUES(NOW(),'')
我的代码是
控制器
public function registerbatch(){
for ($i = 0; $i < count($this->input->post('surname','firstname','age','school','course','email')); $i++) {
$this->form_validation->set_rules("surname[$i]", "surname[$i]", "required");
$this->form_validation->set_rules("firstname[$i]", "firstname[$i]", "required");
$this->form_validation->set_rules("age[$i]", "Age[$i]", "required");
$this->form_validation->set_rules("school[$i]", "School[$i]", "required");
$this->form_validation->set_rules("course[$i]", "Course[$i]", "required");
$this->form_validation->set_rules("email[$i]", "Email[$i]", "required");
}
if ($this->form_validation->run() == TRUE) {
$reg_dat_multi = $this->input->post('surname');
$reg_dat_multi = $this->input->post('name');
$reg_dat_multi = $this->input->post('age');
$reg_dat_multi = $this->input->post('school');
$reg_dat_multi = $this->input->post('course');
$reg_dat_multi = $this->input->post('email');
foreach ($reg_dat_multi as $reg_dat) {
$this->user_model->add_batchuser($reg_dat);
}
$this->load->view('user/home_view');
} else {
$this->load->view('user/batch_register');
}
}
for view
<html>
<head>
</head>
<body>
<form class="form" action="<?php echo base_url() . 'user/registerbatch'; ?>" method="post" class="form-horizontal" role="form">
<?php for ($i = 0; $i < $num; $i++): ?>
<br>
Surname: <input type="text" name="surname[]">
<br>
Name: <input type="text" name="firstname[]">
<br>
Age:<input type ="int" name ="age[]">
<br>
School: <input type="text" readonly value="<?= $school ?>" name="school[]">
<br>
Course:<input type ="text" name ="course[]">
<br>
Email:<input type ="text" name ="email[]">
<br>
<br>
<?php endfor ?>
<button type="submit" class="btn btn-success">Register</button>
</body>
</html>
和模型
public function add_batchuser($reg_dat) {
$this->db->set('date_registered', 'NOW()', FALSE);
$this->db->insert('user', $reg_dat);
}
答案 0 :(得分:0)
为什么不使用codeigniter的insert_batch
?
docs中的示例:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
对于您的用例假设您具有相同数量的姓氏,名字和您可以预先创建数组的所有输入,然后添加使用insert_batch
:
public function add_batchuser($reg_dat) {
foreach ($reg_dat[firstname] as $key => $value) {
$details[$key]['firstname'] = $reg_dat['firstname'][$key];
$details[$key]['surname'] = $reg_dat['surname'][$key];
$details[$key]['age'] = $reg_dat['age'][$key];
$details[$key]['course'] = $reg_dat['course'][$key];
$details[$key]['email'] = $reg_dat['email'][$key];
$details[$key]['date_registered'] => $this->database_now_time();
}
$this->db->insert_batch('user', $details);
}
请删除:
foreach ($reg_dat_multi as $reg_dat) {
$this->user_model->add_batchuser($reg_dat);
}
你可以使用:
$reg_dat_multi = $this->input->post(null, true); //This will get all the data in post parameters
$this->user_model->add_batchuser($reg_dat_multi);