我想创建一个用户注册表格,我允许用户可以把他们想要的电子邮件和电话,就像谷歌联系人。
我已经创建了一个表单,允许用户动态添加或删除电子邮件和电话的输入字段。这是它的图像:用户注册表
这是我使用的模型脚本。。
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');
}
我运行这个代码,它给我错误说数组到字符串的转换,我不能存储任何数据到数据库。任何帮助都将不胜感激。
更新时间:
下面是我得到的错误:
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
我使用的表格:
表\u用户(id、名称、用户名、密码)
表\u电子邮件(id、电子邮件、用户\u id、标签\u id)
表\u phone(id,phone,user\u id,label\u id)
表\u标签(id,label)
2条答案
按热度按时间wydwbb8l1#
对于数组到字符串的转换错误,可以使用以下内爆函数:
请编辑你的帖子,把整个错误,以便我们可以帮助你。
ijnw1ujt2#
在批插入代码之前添加此行
然后在批插入中进行更改,如下所示