2014-07-07 2 views
0

중복 된 항목 오류가 발생하면 사용자를 다른 페이지로 리디렉션하려고합니다. 나는 사용하고있다 Codeigniter중복 항목 오류가 발생하면 사용자를 다른 페이지로 리디렉션하는 방법?

. 다음 코드를 작성했지만 오류 메시지 Duplicate entry '1' for key 'semester_id'을 표시합니다.

친절하게 코드를 확인하고 안내합니다.

감사

public function insert_sports_sports($student_id, $squash, $football, $cricket, $hockey, $swimming, $semester_id) 
{ 
    $data= array (

      "squash"=> $squash, 
      "football"=>$football, 
      "cricket"=>$cricket, 
      "hockey"=>$hockey, 
      "swimming"=>$swimming, 
      "studentId"=>$student_id, 
      "semester_id" => $semester_id 

    ); 

    if ($this->db->insert("sports",$data)) 
    { 
     return true; 
     } 
     else 
     { 
      return false; 
      } 
    } 

CONTROLLER

--- 
-- 
-- 
    if($this->loginmodel->insert_sports_sports($student_id, $squash, $football, $cricket, 
              $hockey, $swimming, $semester_id)) 
    { 
     redirect('mycontroller/index'); 
     } 
     else 
     { 
      $this->load->view('success'); 
      } 
    } 

편집

if ($this->db->insert("sports",$data)) 
    { 
     return true; 
     } 
     else 
     { 
      return false; 
      } 
    } 


catch (Exception $ex) { 

     if($ex->message == "Duplicate entry '1' for key 'semester_id'") 
     { 
      redirect('success.php');  

      }  
} 
} 

오류 메시지

A Database Error Occurred 

Error Number: 1062 

Duplicate entry '1' for key 'semester_id' 

INSERT INTO `sports` (`squash`, `football`, `cricket`, `hockey`, `swimming`, `studentId`, `semester_id`) VALUES ('4', '4', '4', '4', '4', '42', '1') 

Filename: F:\xampp\htdocs\student_management2\system\database\DB_driver.php 

Line Number: 330 
+0

즉, semester_id가 고유 한 키임을 의미합니다. 즉, 해당 열의 값이 1 인 항목이 이미 데이터베이스에 있음을 의미합니다. [This (http://stackoverflow.com/questions/487314/primary-key) -or-unique-index/2916464 # 2916464) 페이지에 설명되어 있습니다. – jeremy

+0

네,하지만이 오류 메시지를 표시하고 싶지 않습니다. 사용자를 다른 페이지로 리디렉션하고 사용자 정의 오류 메시지를 표시하고 싶습니다. – user3480644

답변

3

제거 시도 캐치를 다음과 같이 규칙을 설정하여이를 방지하고이

을 시도 할 수 있습니다
$orig_debug = $this->db->db_debug; 
$this->db->db_debug = FALSE; 

if ($this->db->insert("sports",$data)) 
{ 
    $this->db->db_debug = $orig_debug; 
    return true; 
} 
else 
{ 
    $this->db->db_debug = $orig_debug; 
    return false; 
} 

기본적으로 삽입 쿼리에 대한 디버그를 음소거합니다. 오류가 발생하는 경우 삽입이 수행되지 않으므로 false를 반환합니다.

+0

트릭을 해줘서 고마워! :) – user3480644

3

오류에 리디렉션 만합니다

try { 
    $data= array (

      "squash"=> $squash, 
      "football"=>$football, 
      "cricket"=>$cricket, 
      "hockey"=>$hockey, 
      "swimming"=>$swimming, 
      "studentId"=>$student_id, 
      "semester_id" => $semester_id 

    ); 

    if ($this->db->insert("sports",$data)) 
    { 
     return true; 
     } 
     else 
     { 
      return false; 
      } 
    } 
} 
catch (Exception $ex) { 
    if($ex->message == "Duplicate entry '1' for key 'semester_id'") 
     redirect('yourpage.php'); 
} 
+1

답장을 보내 주셔서 감사합니다. 나는 여전히 같은 오류 메시지가 나타납니다. 친절하게 다시 확인하십시오. – user3480644

+1

내가보기에 예외의 메시지 속성에 액세스하는 것을 잊어 버렸습니다. $ ex-> message를 $ ex-> message로 변경하면 catch 블록에서 $ ex-> message가 비교 될 메시지를 변경하는 것보다 – iamawebgeek

+0

오류가 여전히 있습니다 .. – user3480644

2

당신은 semester_id에 대한

$this->form_validation->set_rules('semester_id', 'input_name', 'trim|required|is_unique[TABLENAME.COLUMNNAME]'); 
+0

+1 대답은 몇 가지 개조가 필요하지만 (그것도 양식이 아니라) 대답을 감사드립니다. –

관련 문제