我有这段代码:
function insertGenre() {
$genres = explode(',',$this->input->post('genreName'));
foreach($genres as $genre) {
$temp = array(
'id' => null,
'name' => $genre,
'popular' => '0'
);
$data2[] = $temp;
}
return $data2;
$data = array(
'id' => null,
'name' => $this->input->post('genreName'),
'popular' => '0'
);
//$this->db->insert('genres',$data);
$this->db->insert_batch('genres',$data2);
}
它应该运行得很好(我在这里遇到了几个问题),但事实并非如此。我做错了什么?
答案 0 :(得分:1)
不应该是这样的:
function insertGenre() {
$genres = explode(',',$this->input->post('genreName'));
foreach($genres as $genre) {
$temp = array(
'id' => null,
'name' => $genre,
'popular' => '0'
);
$data2[] = $temp;
}
if( $this->db->insert_batch('genres',$data2) ) {
return $data2;
} else {
return false;
}
}
您在运行插入查询之前返回了。第二个$data
变量似乎也是多余的。
我还在最后添加了if语句,如果插入失败将返回false。
答案 1 :(得分:0)
您需要将return
移动到函数的末尾,否则它将永远不会执行它下面的行:
function insertGenre() {
$genres = explode(',',$this->input->post('genreName'));
foreach($genres as $genre) {
$temp = array(
'id' => null,
'name' => $genre,
'popular' => '0'
);
$data2[] = $temp;
}
$data = array(
'id' => null,
'name' => $this->input->post('genreName'),
'popular' => '0'
);
//$this->db->insert('genres',$data);
$this->db->insert_batch('genres',$data2);
return $data2;
}