【发布时间】: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:
等等.. 我发现这种方式可能是我的最佳方式.. 有什么建议吗?
【问题讨论】: