2012-08-03 3 views
-2

는이 코드가 폭발에서 insert_batch :CodeIgniter의 및

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

어떻게해야할까요? 출력으로 무엇을 기대합니까? 실제로 무엇을합니까? [무엇을 시도 했습니까?] (http://www.whathaveyoutried.com/)? – Matt

+2

또한 ** 당신의 함수가'$ data2'를'반환 '하고 그 아래의 줄을 결코 실행하지 않는다는 것을 ** 당신은 인식합니까? – Matt

+0

['return'] (http://php.net/manual/en/function.return.php)의 기능을 새로 고침하십시오. (그것은 링크입니다, BTW). – Matt

답변

1

가 이런 식으로 뭔가해야하지 삽입 쿼리를 실행하기 전에 반환했습니다. 두 번째 $data 변수도 중복 된 것처럼 보입니다.

끝에 삽입 문이 실패하면 false를 반환하는 끝에 if 문을 추가했습니다.

0

당신은 그렇지 않으면 그 아래 라인을 실행하지 않습니다, 함수의 끝 부분에 return를 이동해야합니다

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

:

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