我需要在同一列中存储两个值。这些是代码和名称,用连字符分隔。我怎么能用这个数组呢?
'name'=> $data['code']['name']
我读过关于序列化的内容,但我不知道如何在这里实现它。
public function addProduct($code, $name, $photo, $data = array())
{
if($photo == NULL) {
// Product data
$productData = array(
'code' => $data['code'],
'name' => $data['name'],
'category_id' => $data['category_id'],
'subcategory_id' => $data['subcategory_id'],
);
} else {
// Product data
$productData = array(
'code' => $data['code'],
'name' => $data['name'],
'category_id' => $data['category_id'],
'subcategory_id' => $data['subcategory_id'],
'image' => $photo
);
}
if($this->db->insert('products', $productData)) {
return true;
} else {
return false;
}
}
答案 0 :(得分:0)
CodeIgniter中有2个方法可以在数据库中插入数据。
第一种方法:正常的SQL查询
public function addProduct($code, $name, $photo, $data = array()){
$sql = "";
if($photo == null){
// Product data
$d_code = isset($data['code']) ? $this->db->escape($data['code']) : '';
$d_name = isset($data['name']) ? $this->db->escape($data['name']) : '';
$d_category_id = isset($data['category_id']) ? $this->db->escape($data['category_id']) : '';
$d_subcategory_id = isset($data['subcategory_id']) ? $this->db->escape($data['subcategory_id']) : '';
$sql = "INSERT INTO mytable (code, name, category_id, subcategory_id) ";
$sql .= "VALUES ('" . $d_code . "', '" . $d_name . "', '" . $d_category_id . "', '" . $d_subcategory_id . "')";
} else {
// Product data
$d_code = isset($data['code']) ? $this->db->escape($data['code']) : '';
$d_name = isset($data['name']) ? $this->db->escape($data['name']) : '';
$d_category_id = isset($data['category_id']) ? $this->db->escape($data['category_id']) : '';
$d_subcategory_id = isset($data['subcategory_id']) ? $this->db->escape($data['subcategory_id']) : '';
$d_image = isset($photo) ? $this->db->escape($photo) : '';
$sql = "INSERT INTO mytable (code, name, category_id, subcategory_id, image) ";
$sql .= "VALUES ('" . $d_code . "', '" . $d_name . "', '" . $d_category_id . "', '" . $d_subcategory_id . "', '" . $d_image . "')";
}
$this->db->query($sql);
if($this->db->affected_rows() > 0){
return true;
} else {
return false;
}
}
第二种方法:ActiveRecord
public function addProduct($code, $name, $photo, $data = array()){
if($photo == null){
// Product data
$d_code = isset($data['code']) ? $this->db->escape($data['code']) : '';
$d_name = isset($data['name']) ? $this->db->escape($data['name']) : '';
$d_category_id = isset($data['category_id']) ? $this->db->escape($data['category_id']) : '';
$d_subcategory_id = isset($data['subcategory_id']) ? $this->db->escape($data['subcategory_id']) : '';
$dataArr = array(
'code' => $d_code,
'name' => $d_name,
'category_id' => $d_category_id,
'subcategory_id' => $d_subcategory_id
);
} else {
// Product data
$d_code = isset($data['code']) ? $this->db->escape($data['code']) : '';
$d_name = isset($data['name']) ? $this->db->escape($data['name']) : '';
$d_category_id = isset($data['category_id']) ? $this->db->escape($data['category_id']) : '';
$d_subcategory_id = isset($data['subcategory_id']) ? $this->db->escape($data['subcategory_id']) : '';
$d_image = isset($photo) ? $this->db->escape($photo) : '';
$dataArr = array(
'code' => $d_code,
'name' => $d_name,
'category_id' => $d_category_id,
'subcategory_id' => $d_subcategory_id,
'image' => $d_image
);
}
$this->db->insert('mytable', $dataArr);
if($this->db->affected_rows() > 0){
return true;
} else {
return false;
}
}
备注强>
请记住,在这两种方式中,您应该拥有MySQL数据库中的确切结构。否则,您将遇到错误,例如"列未找到"等
另一方面,您无法在MySQL中存储数组,但您可以存储逗号分隔值(CSV)字符串,但根本不建议使用它:
$dataArr = array(
$d_code,
$d_name,
$d_category_id,
$d_subcategory_id
);
$csv = implode(",", $dataArr);
如果提供的任何数据包含逗号,此解决方案将导致问题。
为什么你不应该在MySQL行中存储CSV:
答案 1 :(得分:0)
如果我理解正确,我想你想要这个:
'name' => serialize(array('code'=>$data['code'], 'name'=>$data['name']))
然后要检索它,你可以简单地
$data = unserialize($res['name']);
echo $data['code']."-".$data['name'];
您还可以json_encode()
和json_decode()
使用serialize()
和unserialize()
。使用JSON的好处是可以使用其他语言(如JavaScript)对其进行解码,而seialize()是特定于PHP的函数。