-2

我有这个代码:

    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);
}

它应该运行得很好(我在这里遇到了几个问题),但事实并非如此。我究竟做错了什么?

4

2 回答 2

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。

于 2012-08-03T13:03:03.977 回答
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;
}
于 2012-08-03T12:59:41.053 回答