我想创建一个用户注册表单,我允许用户像Google通讯录一样放置尽可能多的电子邮件和手机。
我已经创建了允许用户动态添加或删除电子邮件和手机输入字段的表单。这是它的形象: User Registration Form
这是我使用的模型脚本..
public function store_email() {
if (! $this->validate_email()) return FALSE;
$this->db->trans_begin();
try {
$this->db->insert($this->table_user, $this);
$id = $this->db->insert_id();
foreach ($this->email as $key => $value) $this->email[$key]['user_id'] = $id;
$this->db->insert_batch($this->table_email, $this->email);
} catch (Exception $e) {
return FALSE;
}
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
public function store_phone() {
if (! $this->validate_phone()) return FALSE;
$this->db->trans_begin();
try {
$this->db->insert($this->table_user, $this);
$id = $this->db->insert_id();
foreach ($this->phone as $key => $value) $this->phone[$key]['user_id'] = $id;
$this->db->insert_batch($this->table_phone, $this->phone);
} catch (Exception $e) {
return FALSE;
}
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
这是我使用的控制器脚本..
public function store() {
$this->model->first_name = $this->input->post('first_name', TRUE);
$this->model->middle_name = $this->input->post('middle_name', TRUE);
$this->model->last_name = $this->input->post('last_name', TRUE);
$this->model->email = [];
$this->model->phone = [];
foreach ($this->input->post('label_id', TRUE) as $key => $value) $this->model->email[$key]['label_id'] = $value;
foreach ($this->input->post('email', TRUE) as $key => $value) $this->model->email[$key]['email'] = $value;
foreach ($this->input->post('label_id', TRUE) as $key => $value) $this->model->phone[$key]['label_id'] = $value;
foreach ($this->input->post('phone', TRUE) as $key => $value) $this->model->phone[$key]['phone'] = $value;
if ($this->model->store_email() === TRUE) {
if ($this->model->store_phone() === TRUE) {
$notification = 'Register success! You may login now!';
} else {
$notification = 'Register fail!';
}
} else {
$notification = 'Register fail!';
}
$this->CI->session->set_flashdata('Success',$notification);
redirect(base_url().'login');
}
我运行此代码,它给我一个错误,说Array to String转换,我无法将任何数据存储到数据库中。 任何帮助都会非常感激。
更新:
这是我得到的错误:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 1496
Backtrace:
File: C:\xampp\htdocs\ci-master-detail-trans\application\models\Customer_model.php
Line: 103
Function: insert_batch
File: C:\xampp\htdocs\ci-master-detail-trans\application\controllers\Customer.php
Line: 50
Function: store
File: C:\xampp\htdocs\ci-master-detail-trans\index.php
Line: 292
Function: require_once
我用于此的表格:
答案 0 :(得分:0)
对于数组到字符串转换错误,您可以使用IMplode函数,如下所示:
$string_version = implode(',', $original_array)
请编辑您的帖子并输入完整错误,以便我们为您提供帮助。
答案 1 :(得分:0)
在batch_insert代码
之前添加此行 $phone_details = implode(',',$this->phone);
然后在批量插入中进行更改,如下所示
$this->db->insert_batch($this->table_phone, $phone_details); // phone number will be stored in , separated format i.e. 1234567890,3216549870