【发布时间】:2013-12-30 15:29:38
【问题描述】:
我正在尝试使用从下拉框中获取的数组在我的数据库中进行搜索,然后在搜索该实例后尝试将其插入到我的表中。这是我到目前为止所拥有的。
控制器:
public function insertTable() {
$text = $this->input->post('text');
$value['value'] = $this->input->post('value');
print_r($value);
$data = $this->myModel->insertTo($value,$text);
}
模型:(注意 table1 有一个自动递增的 id 值,它是 table2 中的外键)
public function insertTo($value,$text){
$this->db->insert('table1', array('text' => $text);
$id = $this->db->insert_id();
foreach ($value as $v) {
$query = $this->db->get_where('Table3', array('value' => $v));
$result = $query->result();
foreach ($result as $row) {
$vID = $row->vID;
}
$this->db->insert('Table2', array('ID' => $id, 'vID' => $vID));
}
}
如您所见,我首先向 table1 插入一个值并在其中获取主键 id 值,然后我有一个 foreach 循环,它循环 $value 数组中的每个值。我在我的数据库中查询它保存值并插入。执行此操作时出现以下错误:
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM (`Table3`) WHERE `value` = Array
Filename: /Applications/MAMP/htdocs/CI/models/myModel.php
Line Number: 24
所以我的问题是我哪里出错了?我应该如何使用一组值查询数据库,然后将其插入数据库?
一个实例的例子是:
$value = 'hello','goodbye','morning';
//lets say when the array value is 0
$query = $this->db->get_where(table3, array('value' => 'hello');
//say this query returns 1
$this->db->insert('Table2', array('ID' => $id, 'vID' => '1');
我希望数组中的每个值都发生这种情况,所以下次我们将通过 goodbye 进行搜索,id 为 2 并将插入到 table2 中
【问题讨论】:
标签: php sql codeigniter