完整性约束违规:1062密钥'PRIMARY'重复输入'1'

时间:2021-10-21 20:24:30

I have a databse problem where i get Integrity constraint violation: 1062. I tried some things on my own but it didtn work so now i am asking you guys to see if you people can help me out.

我有一个数据库问题,我得到完整性约束违规: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));
}

when i run this the first INSERT INTO doesnt fill in any data and giving this error: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' there already is a primary 1 key in there. but it is on auto increment. in the tekst tabel the item_id gets an 0 input.

当我运行这个时,第一个INSERT INTO不会填写任何数据并给出此错误:完整性约束违规:1062重复条目'1'表示密钥'PRIMARY'已经有一个主要1密钥。但它是自动增量。在tekst tabel中,item_id获得0输入。

Javascript:

使用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:

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>

Hope someone can help me on here. already alot of thanks in advance. :)

希望有人可以帮助我。已经很多人提前感谢了。 :)

4 个解决方案

#1


12  

When inserting into a table with an auto increment field, the auto increment field itself should not be specified at all.

当插入具有自动增量字段的表时,根本不应指定自动增量字段本身。

Query("INSERT INTO prod_categorie (categorieID, parentID) VALUES (?, ?)", array($chkParent, $txtParentCategorie));
                                   ^^^^^^^^^^^                    ^             ^^^^^^^^^^

Should be just

应该只是

Query("INSERT INTO prod_categorie (parentID) VALUES (?)", array($txtParentCategorie));

Just added as answer from comment discussion to allow accept and finishing the question.

刚刚添加评论讨论的答案,以允许接受和完成问题。

#2


5  

in my case the error is:

在我的情况下,错误是:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

SQLSTATE [23000]:完整性约束违规:1062键'PRIMARY'重复输入'0'

the solution is to Empty/Truncate all records of the table in question

解决方案是清空/截断相关表的所有记录

The problem happens when auto-increment is disabled on the primary-key of that table or the data-type is wrong.

当在该表的主键上禁用自动增量或数据类型错误时,会发生此问题。

partially credited to https://magento.stackexchange.com/questions/56354/admin-error-sqlstate23000-integrity-constraint-violation-1062-duplicate-ent

部分记入https://magento.stackexchange.com/questions/56354/admin-error-sqlstate23000-integrity-constraint-violation-1062-duplicate-ent

#3


1  

I had the same problem, and it was not the auto increment that was causing it. I changed the data type on my table ID from TINYINT(3) to INT(10). Try that. Maybe it'll help.

我有同样的问题,并不是引起它的自动增量。我将表ID上的数据类型从TINYINT(3)更改为INT(10)。试试吧。也许它会有所帮助。

#4


0  

I came across this problem when using Magento 2 with the Google Experiment set to Yes. Simply shutting it off solved my page save issue. Bu ti'm still having a problem with adding catalog products it give me an error that the URL Key for the specified store already exists. and the image is not uploading even though i have correct folder permissions. Will post an update in case it helps anyone else.

当使用Magento 2并将Google Experiment设置为Yes时,我遇到了这个问题。简单地关闭它解决了我的页面保存问题。如果添加目录产品仍然存在问题,则会给我一个错误,即指定商店的URL密钥已经存在。即使我有正确的文件夹权限,图像也不会上传。将发布更新,以防它帮助其他任何人。

#1


12  

When inserting into a table with an auto increment field, the auto increment field itself should not be specified at all.

当插入具有自动增量字段的表时,根本不应指定自动增量字段本身。

Query("INSERT INTO prod_categorie (categorieID, parentID) VALUES (?, ?)", array($chkParent, $txtParentCategorie));
                                   ^^^^^^^^^^^                    ^             ^^^^^^^^^^

Should be just

应该只是

Query("INSERT INTO prod_categorie (parentID) VALUES (?)", array($txtParentCategorie));

Just added as answer from comment discussion to allow accept and finishing the question.

刚刚添加评论讨论的答案,以允许接受和完成问题。

#2


5  

in my case the error is:

在我的情况下,错误是:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

SQLSTATE [23000]:完整性约束违规:1062键'PRIMARY'重复输入'0'

the solution is to Empty/Truncate all records of the table in question

解决方案是清空/截断相关表的所有记录

The problem happens when auto-increment is disabled on the primary-key of that table or the data-type is wrong.

当在该表的主键上禁用自动增量或数据类型错误时,会发生此问题。

partially credited to https://magento.stackexchange.com/questions/56354/admin-error-sqlstate23000-integrity-constraint-violation-1062-duplicate-ent

部分记入https://magento.stackexchange.com/questions/56354/admin-error-sqlstate23000-integrity-constraint-violation-1062-duplicate-ent

#3


1  

I had the same problem, and it was not the auto increment that was causing it. I changed the data type on my table ID from TINYINT(3) to INT(10). Try that. Maybe it'll help.

我有同样的问题,并不是引起它的自动增量。我将表ID上的数据类型从TINYINT(3)更改为INT(10)。试试吧。也许它会有所帮助。

#4


0  

I came across this problem when using Magento 2 with the Google Experiment set to Yes. Simply shutting it off solved my page save issue. Bu ti'm still having a problem with adding catalog products it give me an error that the URL Key for the specified store already exists. and the image is not uploading even though i have correct folder permissions. Will post an update in case it helps anyone else.

当使用Magento 2并将Google Experiment设置为Yes时,我遇到了这个问题。简单地关闭它解决了我的页面保存问题。如果添加目录产品仍然存在问题,则会给我一个错误,即指定商店的URL密钥已经存在。即使我有正确的文件夹权限,图像也不会上传。将发布更新,以防它帮助其他任何人。