【问题标题】:Codeigniter Insert Array to DatabaseCodeigniter 将数组插入数据库
【发布时间】:2015-08-07 00:23:37
【问题描述】:

我在 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;
}

但显然“电话”的代码是错误的,我只是不知道如何正确地做到这一点。

【问题讨论】:

    标签: arrays codeigniter insert


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如下修改你的函数,它会像魅力一样工作,

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

      修改:

      哦,是的,您必须编辑传递给 SaveForm 函数的 para 数组。 请使用下面的代码,忽略上面的代码:

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

      【讨论】:

      • 不幸的是,这个不起作用,并且数组的每一行都在抛出错误Illegal string offset 'first_name' 谷歌搜索这似乎是 PHP 5.4 中出现的常见问题。我正在使用 5.5.8
      • 我已修改答案,请将您的代码替换为发布在“已修改”文本下方的代码!
      • 我知道你在做什么......但它不像 Vinie 建议的那样使用 implode() 简单,所以我接受他的回答。
      【解决方案3】:

      在控制器中

      $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(提交后)

      【讨论】:

      • 我错过了什么?我实际上已经尝试过类似的东西,我的和你的都会产生同样的错误。遇到 PHP 错误严重性:通知消息:数组到字符串的转换文件名:bfmysqli/bfmysqli_driver.php 行号:509 发生数据库错误错误号:1054 '字段列表'中的未知列'数组' INSERT INTO customers (@ 987654328@,last_namephone) 值('franky','smith',数组)
      【解决方案4】:

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

      【讨论】:

      • 谢谢你,但正如 Bugfixer 指出的那样,你的答案不是使用 Codeinigiter,这是我遇到问题的地方。我已经可以用普通 PHP 做到了。
      【解决方案5】:

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

      【讨论】:

        【解决方案6】:
        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();
                    }
                }   
        
            }
        

        【讨论】:

        • 虽然您的代码可能会提供问题的答案,但请在其周围添加上下文,以便其他人了解它的作用以及它存在的原因。
        猜你喜欢
        • 1970-01-01
        • 2018-03-09
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 2015-08-30
        • 2015-08-04
        • 1970-01-01
        • 2013-04-04
        相关资源
        最近更新 更多