完整性约束违规:1062重复输入'1',键名为'PRIMARY'。

17

我有一个数据库问题,出现了完整性约束冲突:1062。我自己尝试了一些方法,但都失败了,所以现在我想向你们寻求帮助。

elseif($action == 'add') {
if($_POST['create'] == true) {
    $title = $_POST['txtTitle'];
    $txtParentCategorie = $_POST['txtParentCategorie'];
    $txtContent = $_POST['txtContent'];

    if($txtParentCategorie == "niks") {
        $txtParentCategorie = NULL;
        $chkParent = 1;
        $order_count = countQuery("SELECT categorieID FROM prod_categorie WHERE parentID=?",array(1));
        $order = $order_count + 1;
    } else {
        $chkParent = null;
        $order_count = countQuery("SELECT categorieID FROM prod_categorie WHERE parentID is not NULL");
        $order = $order_count + 1;
    }

    Query("INSERT INTO prod_categorie (categorieID, parentID) VALUES (?, ?)", array($chkParent, $txtParentCategorie));
    $inserted_id = getLastInsertId();
    Query("INSERT INTO tekst (tabel, kolom, item_id, tekst, taalID) VALUES(?, ?, ?, ?, ?)", array('prod_categorie', 'categoriename', $inserted_id, $title, $lang));
    Query("INSERT INTO tekst (tabel, kolom, item_id, tekst, taalID) VALUES(?, ?, ?, ?, ?)", array('prod_categorie', 'content', $inserted_id, $txtContent, $lang));
    $languages = selectQuery("SELECT taalID FROM taal WHERE taalID!=?",array($lang));
}
当我运行这段代码时,第一个INSERT INTO没有填充任何数据,并报错:完整性约束冲突:1062键 'PRIMARY' 中的重复条目'1'。表中已经有一个主键为1的自增值。在'tekst'表中,'item_id'会得到一个0的输入。
Javascript:
    $('.btnAddCategorie').click(function(){
    if(busy != 1){
        busy = 1;
        var error = 0;
        var gallery = $('select[name="gallery_dropdown"]').val();
        if($('input[name="txtTitle"]').val() == ''){
            error = 1;
            alert('Het titel veld is nog leeg');
            $('input[name="txtTitle"]').focus();
        }
        if(error != 1){
            $('.content_load_icon').html('<img src="../../includes/images/layout/load_small.gif" />');
            var content = $('#cke_ckeditor').children().children().children()[3].contentWindow.document.childNodes[1].childNodes[1].innerHTML;
            $.ajax({
                url: '../../action/ac_productbeheer.php?a=add',
                type: 'POST',
                data: {txtTitle: $('input[name="txtTitle"]').val(), txtForm: $('select[name="txtForm"]').val(), customGalTitle: $('.txtCustomGalleryTitle').val(), gallery_dropdown: gallery, txtParentCategorie: $('select[name="txtParentCategorie"]').val(), txtContent: content, txtMeta: $('.txtMetaDesc').val(), create: true},
                success: function(data, textStatus, xhr) {
                    $('.content_load_icon').html('');
                    $('.txtContentConsole').html('Product succesvol opgeslagen!').show().delay(2000).fadeOut(200);
                    busy = 0;
                    saved = 1;
                    window.location = '../../modules/productbeheer/index.php';
                },
                error: function(xhr, textStatus, errorThrown) {
                    $('.content_load_icon').html('');
                    $('.txtContentConsole').html('Fout bij opslaan! Probeer het later nog een keer.').show().delay(2000).fadeOut(200);
                    busy = 0;
                }
            });
        } else {
            error = 0;
            busy = 0;
        }
    }
});

html:

<a  class="btnAddCategorie"><img name="btnOpslaan" src="/'.CMS_ROOT.'/includes/images/layout/opslaan.png" /></a><span  class="content_load_icon"></span><span  class="txtContentConsole"></span>

希望有人可以在这里帮助我。 提前感谢大家。


1
除非您向我们展示有关该表的创建语句,否则我们无法为您提供帮助。 - N.B.
1
你有三个插入操作。你能说出哪一个失败了吗? - asantaballa
1
你是否试图将一个值插入到主键中?如果是这样,请不要这样做(在文本表中,item_id会得到0的输入)。->同时发布您的表定义。 - AgRizzo
2
你不应该在自增字段中插入值。具体来说,如果prod_categorie中的categorieID是自增的,那么你不应该在categorieID中插入值。 - AgRizzo
4
好的。如果categorieID是自增的,那么我认为在你的插入语句中不应该包含"categorieID"。即使没有在插入语句中指定,系统也会为你创建它。 - asantaballa
显示剩余6条评论
7个回答

18

当向具有自动增量字段的表中插入数据时,不应该指定自动增量字段本身。

Query("INSERT INTO prod_categorie (categorieID, parentID) VALUES (?, ?)", array($chkParent, $txtParentCategorie));
                                   ^^^^^^^^^^^                    ^             ^^^^^^^^^^
应该只是这样
Query("INSERT INTO prod_categorie (parentID) VALUES (?)", array($txtParentCategorie));

刚刚根据评论讨论添加为答案,以允许接受并完成问题。


11

是的。当主键上禁用自增时,问题就会出现。 - user13239154

4

我曾经遇到过同样的问题,但并不是自增引起的。我将表格ID的数据类型从TINYINT(3)更改为INT(10)。你可以试试这个方法,或许会有所帮助。


为了对这个答案进行一些验证,我来说一下我的问题。我的表已经设置了auto_increment,但是我没有指定主键,但我仍然遇到了错误。看到我的字段类型设置为 tinyint 而不是 int。这个答案对我很有帮助,谢谢! - ckpepper02

0
   public function collection(Collection $rows)
   {
        foreach ($rows as $row) 
        {
            $id = $row[0];
            $mail = $row[0];
            if($id){
                DB::table('mdl_user')->where('email','=',$mail)->update(['firstname'=>$id]);
                return response()->json("data updated", 200);
            }
            else{
                return response()->json("No data updated", 401);
            }
        }
   }

任何能让提问者朝着正确方向前进的答案都是有帮助的,但请尽量在回答中提及任何限制、假设或简化。简洁明了是可以接受的,但更充分的解释会更好。请参考如何撰写优秀答案。请提供一些关于如何回答原帖问题的解释。 - Kuro Neko

0

当我使用Http:put和Http:patch时,我遇到了相同的问题。因此,问题出在我的算法上。

我试图在hero table中保存一个重复的ID,请看:

 public function updateHero(Request $request){
    
    $id =hero::find($request->id);

    if($id){
        $theHero=new hero;
         $theHero->id=$request->id;
        $theHero->name=$request->name;
        $theHero->save();

        return response()->json("data updated", 200);
    }
    else{
        return response()->json("No data updated", 401);
    }
}

所以我在我的代码中删除了$theHero->id=$request->id;

public function updateHero(Request $request){
    
    $id =hero::find($request->id);

    if($id){
        $theHero=new hero;
        $theHero->name=$request->name;
        $theHero->save();

        return response()->json("data updated", 200);
    }
    else{
        return response()->json("No data updated", 401);
    }
}

0
只需在ID字段部分的复选框A_I上打勾。
祝好运。

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

0

在使用Magento 2并将Google实验设置为“是”时,我遇到了这个问题。简单地关闭它解决了我的页面保存问题。但是,在添加目录产品方面,我仍然有一个问题,它会给我一个错误,指定的商店的URL键已经存在,即使我具有正确的文件夹权限,图像也无法上传。如果有帮助其他人的情况,我会发布更新。


嗨,约书亚, 想到我可以补充一下(如果你还没有检查),我遇到了非常相似的问题,结果发现我的“catalog_product_entity_int”表已经达到了“int”类型的最大值,因此在我尝试创建新的简单产品时出现了奇怪的问题。我理解我正在处理的是Magento 1,但原则是相同的,也许问题是底层数据类型的限制。 - Nathaniel Rogers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接