2014-10-21 5 views
1

아래 양식을 사용하여 데이터베이스의 데이터를 업데이트하면 다음 오류 오류가 표시됩니다. 이 오류를 어떻게 해결할 수 있습니까?CodeIgniter로 데이터베이스의 데이터를 업데이트하는 방법은 무엇입니까?

Cannot add or update a child row: a foreign key constraint fails 
(`nov-cms`.`faqs_cat`, CONSTRAINT `faqs_cat_ibfk_20` FOREIGN KEY (`id`) 
REFERENCES `tblfaqs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)' 

형태 :

function editpage_($editid) { 
     $conn = Doctrine_Manager::connection(); 
     $data['headingtop'] = "FAQ'S > FAQ'S > Edit FAQ'S"; 
     $data['title_faqs'] = "Edit FAQ's"; 




     $data['errormess'] = ''; 
     $title_faqs = addslashes($this->input->post('title_faqs')); 
     $content = addslashes($this->input->post('editor1')); 
     $seo_description = addslashes($this->input->post('seo_description')); 
     $category = $this->input->post('category'); //print_r($category['cate']); exit; 
     $isActive = $this->input->post('isactive'); 
     $position = $this->input->post('position'); 
$qryDel2 = $conn->prepare("DELETE from faqs_cat where id='$editid'"); 
    $qryDel2->execute(); 


$query = $conn->prepare("UPDATE tblfaqs SET title_faqs='$title_faqs',content='$content',isActive='$isActive',position='$position',seo_description='$seo_description' WHERE id='$editid' "); 
     $query->execute(); 
     $id = $conn->lastInsertId(); 
     //when we add a page this insert a record to table, and this is id of record inserted.. this is a function default of mysql, we use to get last insert id to insert this to orther table... 
     foreach ($category as $data) { 
      $query = $conn->prepare("insert into faqs_cat (fc_id, id, category_id) values ('','$id','$data')"); //in this. 
      $query->execute(); 
     } 
$url = 'admin/faqs/'; 
     echo' 
<script> 
window.location.href = "' . base_url() . $url . '"; 
</script> 
'; 
+0

코드 및 오류는 표시되지만 질문은 없습니다. – Celeo

+0

Question 업데이트 된 @celeo –

+0

오류가 아주 명확합니다. forening 키를 변경하는 행을 수정하려고하고 새 외래 키가 부모 테이블에 존재하지 않습니다. –

답변

0

오류가이 라인이다 lastInsertId()이 값을 반환하지 않기 때문에

$id = $conn->lastInsertId(); 

구체적 $id가 비어. 이로 인해 faqs_cat에 ID가 필요하므로 제약 조건이 실패하게됩니다.

데이터베이스에서 자동 생성 된 키를 얻으려는 경우 연결할 데이터베이스 유형에 따라 시퀀스 이름을 lastInsertId으로 지정해야 할 수 있습니다 (예 : Postgres에는 시퀀스 이름 필요).). 'mySequence'라는 시퀀스를 작성하고 대신 다음을 사용하십시오.

$id = $conn->lastInsertId('mySequence'); 
관련 문제