【问题标题】:Insert array in ONE field in mysql在mysql的一个字段中插入数组
【发布时间】:2010-02-01 05:37:47
【问题描述】:

我想使用 PHP 在 mysql 数据库的 ONE 字段中插入一个数组..

这很好:

HTML:

anotherField :<input type="text"  name="anotherField" />

fax :<input type="text"  name="f[]" />
email :<input type="text"  name="f[]" />
phone :<input type="text"  name="f[]" />

PHP(我使用 CodeIgniter 框架):

<?php
    function addCustomRow($tableName)
    {

    $arr = $this->input->post('f');
    $field = implode("|", $arr);

        $data = array(
        'anotherField'          => $this->input->post('anotherField'),
        'field'             => $field
        );

        $this->db->insert($tableName, $data);
    }
?>

我像这样在 mysql 中获取数据

fax|email|phone

但是..

我的问题是..我想在同一个字段中有很多数组..像这样:

fax|email|phone :br: fax|email|phone :br: fax|email|phone ..

我尝试过这样的事情:

HTML:

First array :
fax :<input type="text" class="inp" name="f[0][0]" />
email :<input type="text" class="inp" name="f[0][1]" />
phone :<input type="text" class="inp" name="f[0][2]" />

Second array :
fax :<input type="text" class="inp" name="f[1][0]" />
email :<input type="text" class="inp" name="f[1][1]" />
phone :<input type="text" class="inp" name="f[1][2]" />

PHP:

<?php
    function addCustomRow($tableName)
    {

    $arr = $this->input->post('f[]');
    $field = implode(":br:", $arr);

        $data = array(
        'anotherField'          => $this->input->post('anotherField'),
        'field'             => $field
        );

        $this->db->insert($tableName, $data);
    }
?>

但它显示错误 [严重性:通知消息:数组到字符串转换] 我像这样在mysql中获取数据

array :br: array  

编辑:

我想这样做,因为我有一个类别表.. 每只猫都有自己的详细信息(字段)..所以当我添加新猫时,我只是做这样的事情

       name of cat : <input type="text"  name="name" />

   <!-- Fields -->
    Fields //

    Field 1 :
    title of the filde : <input type="text"  name="f[0][0]" />
    type : <input type="text"  name="f[0][1]" /> <!-- 1= text , 2= select , 3= textarea ..  -->
    default value : <textarea rows="8" cols="20" name="f[0][2]"> </textarea> <!-- if select type write value1::value2::value3  ... -->



Field 2 :
title of the filde : <input type="text"  name="f[1][0]" />
type : <input type="text"  name="f[1][1]" /> <!-- 1= text , 2= select , 3= textarea ..  -->
default value : <textarea rows="8" cols="20" name="f[1][2]"> </textarea> <!-- if select type write value1::value2::value3  ... -->

Field 3 :
title of the filde : <input type="text"  name="f[2][0]" />
type : <input type="text"  name="f[2][1]" /> <!-- 1= text , 2= select , 3= textarea ..  -->
default value : <textarea rows="8" cols="20" name="f[2][2]"> </textarea> <!-- if select type write value1::value2::value3  ... -->

我可以在这里添加任意数量的字段..

在数据库中我希望像这样插入数据:

[nameOfBook|1|anyName :br: noOfPages|1|anyNo ]

例如在另一只猫中:

[colorOfcar|2|red::black::green:br: price|1|anyPrice]

有什么帮助吗?

提前谢谢..


@贾斯汀·约翰逊

感谢您的回答,但它不起作用。 我必须使用 $data var 插入所有数据,但我使用你这样回答

function addCustomRow($tableName)
{

    $data = array(
        'name'          => $this->input->post('name'),
        'fields'            => serialize($this->input->post('f[]'))
        );

    $this->db->insert($tableName, $data);
}

我得到这样的数据 mysqyl (b:0;)!!

//

@Suku 谢谢..我在问之前用过它,但我不知道怎么用..我在这里的情况下如何使用它? ..

@亚历克斯

因为我有一个名为 (categories) 的表,而且每只猫都有自己的字段,例如:

carsCat >
  type :
  color:
  details:

BooksCat >
nameOfbook:
writer:
numberOfpage:

等等.. 我发现这种方式可能是我的最佳方式.. 有什么建议吗?

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    要在一个字段中存储一个数组,只需serialize 它。当你需要从数据库unserialize访问数组时。

    【讨论】:

    • 序列化和反序列化可以解决问题,正如贾斯汀·约翰逊(Justin Johnson)也提到的,将它们标准化也很好
    • 谢谢..我在问之前用过它,但我不知道怎么用..在我的情况下如何使用它?
    【解决方案2】:

    首先,我建议您尝试normalize 您的表方案多一点。在一个字段中存储多个、多个值会引起很多麻烦。或许,是这样的:

    在此方案中,contact_information 表通过将 ID(外部引用)存储到人员行来与 person 表相关联。这样一来,您就可以为任何给定的人创建尽可能多的联系人条目,而不必将一组数据塞进一个字段中。


    无论如何,要按原样解决您的问题,请在将数据插入数据库之前尝试序列化数据。

    function addCustomRow($tableName) {    
        $data = array(
            'anotherField' => $this->input->post('anotherField'),
            'field'        => serialize($this->input->post('f[]'))
        );
    
        $this->db->insert($tableName, $data);
    }
    

    编辑:更新为地址评论。

    【讨论】:

    • 感谢您的回答.. umm 但它不起作用.. 我必须使用 $data var 来插入所有数据,但我使用您的回答就像这个函数 addCustomRow($tableName) { $data =数组('name' => $this->input->post('name'), 'fields' => serialize($this->input->post('f[]')) ); $this->db->insert($tableName, $data);我得到这样的数据 mysqyl ( b:0; ) !!
    • 对不起,复制粘贴到错误的地方。我已经用更正的代码更新了我的答案。
    • 谢谢..我使用了你的新代码,但它也说错了(发生数据库错误错误号:1054 Unknown column 'anotherField' in 'field list' INSERT INTO cats (anotherField, field) 值 (0, 'b:0;') )
    • 哦,对不起! ..我将字段的名称更改为 (name) ,插入了数据,但我在我的字段中得到了 (b:0;) :(
    【解决方案3】:

    您可能需要使用函数 serialize() 和 unserialize(),如下所示:

    $string_from_array = serialize($array);
    

    当你需要恢复数组时:

    $array= unserialize($string_from_array);
    

    更多信息请参见:http://www.wpfasthelp.com/insert-php-array-into-mysql-database-table-row-field.htm

    【讨论】:

      【解决方案4】:

      我用一个小技巧做到了:) 我只是添加一个带值 (:br:) 的 heddin 输入,并保持所有输入名称相等 (f[]) .. 像这样:

      HTML:

      field out of the array :
      anotherField :<input type="text"  name="name" />
      
      Field 1 :
      title of the field : <input type="text"  name="f[]" />
      type : <input type="text"  name="f[]" /> <!-- 1= text , 2= select , 3= textarea ..  -->
      value : <textarea rows="8" cols="20" name="f[]"> </textarea> <!-- if select type write value1::value2::value3  ... -->
      <input type="hidden" name="f[]" value=":br:" />
      
      Field 1 :
      title of the field : <input type="text"  name="f[]" />
      type : <input type="text"  name="f[]" /> <!-- 1= text , 2= select , 3= textarea ..  -->
      value : <textarea rows="8" cols="20" name="f[]"> </textarea> <!-- if select type write value1::value2::value3  ... -->
      <input type="hidden" name="f[]" value=":br:" />
      

      PHP:

      function addCustomRow($tableName)
      {
      $arrF1 = $this->input->post('f');
      $f = implode("|", $arrF1);
      
      $data = array(
          'name'  => $this->input->post('name'),
              'fields'    => $f
          );
      
      $this->db->insert($tableName, $data);
      

      }

      现在我像这样在 mysql 中获取数据:

      titleOffield|type|value|:brr:|titleOffield|type|value|:brr:
      

      例如:

      nameOfBook|1|writeTheName|:Br:|NoOfPages|1|value|:br:|
      

      然后我可以像这样轻松地从 mysql 数据库返回数据: (我用 Smarty!)

      输出

        {assign var=fieldsBr value=":br:|"|explode:$r.values} <!-- exploade :br: -->
      
          {foreach from=$fieldsBr item=ff}
          {assign var=f value="|"|explode:$ff} <!-- exploade | -->
          {$f[0]} :  <b>{$f[2]}</b>  <br />  <!-- title of field :  the value  <br />  -->
          {/foreach}
      

      ** 注意我在这里使用 2 explode 一个用于 (:br:|) 另一个用于 (|) ..

      感谢您的帮助..

      艾哈迈德..

      【讨论】:

        【解决方案5】:

        将数组值从表单插入数据库

        function addRecord($Table){
                global $connection; // database connection
                extract($Table);
        
                $entry_date = date('Y-m-d');
        
                for($i=0; $i< count($project_id); $i++)
                {
        
                    $sql="INSERT INTO expenses(type_id,project_id,amount,exp_date,entry_date,comments,entry_name)
                      values('$type_id[$i]','$project_id[$i]','$amount[$i]','$exp_date[$i]','$entry_date','$comments[$i]','$_SESSION[emp_name]')";
        
                    $result=mysqli_query($conn,$sql);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-11
          • 1970-01-01
          • 1970-01-01
          • 2011-09-30
          • 2018-11-03
          • 1970-01-01
          • 2017-04-26
          • 2015-10-22
          相关资源
          最近更新 更多