mysql - 违反完整性约束 : 1062 Duplicate entry '1' for key 'PRIMARY'

标签 mysql duplicates mysql-error-1062

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

希望有人可以在这里帮助我。 已经提前非常感谢了。 :)

最佳答案

当插入到一个带有自动递增字段的表中时,自动递增字段本身根本不应该被指定。

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

应该只是

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

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

关于mysql - 违反完整性约束 : 1062 Duplicate entry '1' for key 'PRIMARY' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19359120/

相关文章:

让我挠头的 MySql 1062 错误

php - 在一个查询中从多个表中选择记录

Python->数据框->扩展数据框

duplicates - 如何合并购物车中的重复页面?

mysql - #1062 - key '' 的重复条目 'primary'

MySQL 错误 : #1062 - Duplicate entry 'rewrite_rules' for key 2

mysql - 使用 MySQL-cluster 存储的表数据到底存储在哪里?

php - 如何显示带有预选选项的下拉列表

php - 创建数据库表的文件

ios - Tesseract libtesseract_all.a(svpaint.o) 中 _main 的重复符号