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所以保持为空。lastInsertId()这会导致约束失败,因为需要插入faqs_catID。

如果您尝试从数据库中获取自动生成的密钥,您可能需要指定序列名称,lastInsertId具体取决于您连接到的数据库类型(例如,Postgres 需要序列名称)。尝试创建一个名为“mySequence”的序列并改用它:

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