1

以下のフォームを使用してデータベースのデータを更新すると、次のエラーが表示されます。このエラーを解決するにはどうすればよいですか?

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>
';
4

1 に答える 1

0

エラーは次の行にあります。

$id = $conn->lastInsertId();

具体的には、は値を返さない$idため、空のままです。これにより、 IDへのlastInsertId()挿入が必要な場合に制約が失敗します。faqs_cat

データベースから自動生成されたキーを取得しようとしている場合、lastInsertId接続しているデータベースの種類によっては、シーケンス名を指定する必要がある場合があります (たとえば、Postgres にはシーケンス名が必要です)。「mySequence」というシーケンスを作成し、代わりにこれを使用してみてください。

$id = $conn->lastInsertId('mySequence');
于 2014-10-28T21:53:32.870 に答える