我在 Codeigniter 中创建了一个表单,其中包含一个使用 JavaScript 动态复制的电话号码字段。所以基本上我可以有一个或多个这样的字段。
<input name="phone[]" value=""type="text">
<input name="phone[]" value=""type="text">
然后在我的控制器中我有
$form_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $this->input->post('phone[]')
);
然后我将其保存到我的数据库中
function SaveForm($form_data)
{
$this->db->insert('customers', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
但显然“电话”的代码是错误的,我只是不知道如何正确执行此操作。
您无法将数组保存到数据库中。您可以使用
implode()
将其转换为字符串,然后在需要时使用 explode()
将其转换回数组。就像下面这样
$phone=implode(',',$this->input->post('phone'));
$form_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $phone
);
或
您可以将其转换为 json 字符串,并在需要时转换回数组,如下所示:
$phone = json_encode($this->input->post('phone'));
转换回数组
$phone = json_decode($phone, TRUE);
按如下方式修改你的函数,它将像魅力一样工作:
function SaveForm($form_data)
{
$batch_data = [];
foreach ($form_data as $data) {
$batch_data[] = [
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'phone' => $data['phone']
];
}
$this->db->insert_batch('customers', $batch_data);
return $this->db->affected_rows() > 0;
}
在控制器中
$phone = $_POST['phone'];//this will store data as array. Check image 02
$form_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'phone' => $phone,//some times it works with '$phone'
);
在模型中
function SaveForm($form_data)
{
$this->db->insert('customers', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
else
{
return FALSE;
}
}
已测试
图片01(我的表格)
图片02(提交后)
mysql没有任何数组数据类型。所以我们不能将数组直接存储到mysql数据库中。 为此,我们必须首先使用 php serialize() 函数将数组转换为字符串,然后将其保存到 mysql 数据库中。
例如:将数组存储在数据库中的php代码
$array = array("foo", "bar", "hello", "world");
$conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db("mysql_db",$conn);
$array_string=mysql_escape_string(serialize($array));
从数据库检索数组
$conn=mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db("mysql_db",$conn);
$q=mysql_query("select column from table",$conn);
while($rs=mysql_fetch_assoc($q))
{
$array= unserialize($rs['column']);
print_r($array);
}
要将数组插入到数据库,请在 codeigniter 控制器中使用此程序=>
$inputdata=$this->input->post();
$phone=array($inputdata['phone']);
foreach($phone as $arr)
{
$phoneNo=$arr;
$f=count($phoneNo);
for($i=0;$i<$f;$i++)
{
$arre=[
'phone'=>$phoneNo[$i],
];
$insertB= $this->user_model->userdata($arre);
}
}
public function add_theme_pages(){
$page_name = $this->input->post('page');
$page_img = $this->input->post('page_img');
for($i=0; $i < count($page_name); $i++){
$pages_data = array(
'theme_id' => $this->input->post('theme_id'),
'theme_page_name' => $page_name[$i],
'theme_page_img' => $page_img[$i]
);
if($this->backendM->add_theme_pages($pages_data)){
$this->session->set_flashdata('message', 'Theme Added Successfully !');
$this->session->set_flashdata('message_class', 'green');
$this->create_template();
}else{
$this->create_template();
}
}
}