Codeigniter将数组插入数据库

时间:2015-08-07 00:23:38

标签: arrays codeigniter insert

我在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[]')
    );

然后我就这样把它保存到我的dabase

function SaveForm($form_data)
{
    $this->db->insert('customers', $form_data);
    if ($this->db->affected_rows() == '1')
    {
        return TRUE;
    }
    return FALSE;
}

但显然“手机”的代码是错误的,我只是想弄清楚如何正确地做到这一点。

6 个答案:

答案 0 :(得分:5)

您无法将数组保存到数据库中。您可以使用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
        );

OR

您可以将其转换为json字符串,并在需要转换回数组时如下所示:

$phone = json_encode($this->input->post('phone'));

转换回数组

$phone = json_decode($phone, TRUE);

答案 1 :(得分:3)

Modify your function as below and it will works like charm,

function SaveForm($form_data)
    {
        foreach ($form_data as $contact)
        {
            $data[] = array(
                'first_name' => $contact['first_name'],
                'last_name' => $contact['last_name'],
                'phone' => $contact['phone']
                );
        }

        $this->db->insert_batch('customers', $data); 

        if ($this->db->affected_rows() > 0)
        {
            return TRUE;
        }
        return FALSE;
    }

Modified:

Oh, yes you have to edit para array that you passed to SaveForm function. Please use following code, ignore above code:

    foreach($_POST['first_name'] as $key=>$fname) 
    {
        $form_data[] = array(
            'first_name' => $_POST['first_name'][$key],
            'last_name' => $_POST['last_name'][$key],
            'phone' => $_POST['phone'][$key],
            );
    }

function SaveForm($form_data)
  {
    $this->db->insert_batch('customers', $data); 

    if ($this->db->affected_rows() > 0)
    {
        return TRUE;
    }

    return FALSE;
  }

答案 2 :(得分: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 (我的表格)

01

图片02 (提交后)

enter image description here

答案 3 :(得分:0)

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);
}

答案 4 :(得分:0)

将数组插入到数据库中,请在 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);      
           }
       }

答案 5 :(得分:-1)

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();
            }
        }   

    }