【问题标题】:Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'完整性约束违规:1062 键 'PRIMARY' 的重复条目 '1'
【发布时间】:2013-10-21 22:52:00
【问题描述】:

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

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

【问题讨论】:

  • 除非您向我们展示相关表的创建语句,否则我们无法为您提供帮助。
  • 你有三个插入。你能看出哪一个失败了吗?
  • 您是否要在主键中插入一个值?如果是这样 - 不要(在 tekst 表中 item_id 得到一个 0 输入。) -> 同时发布你的表定义
  • 您不应在自动增量字段中插入值。具体来说,如果是自动增量,则不应在 prod_categorie 的 categorieID 中插入值
  • 好的。然后,如果 categorieID 是自动增量,那么我认为 not 根本不应该出现在您的插入语句中。即使不在插入语句中,系统也会为您创建它。

标签: mysql duplicates mysql-error-1062


【解决方案1】:

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

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

应该只是

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

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,不是自动增量导致的。我将表 ID 上的数据类型从 TINYINT(3) 更改为 INT(10)。试试看。也许会有所帮助。

    【讨论】:

    • 只是为了验证这个答案,这是我的问题。我的表设置了 auto_increment 并且我没有指定主键,但我仍然收到错误消息。看到我的字段类型设置为tinyint 而不是int。这个答案对我有帮助,谢谢!
    【解决方案3】:

    在我的情况下,错误是:

    SQLSTATE[23000]:违反完整性约束:1062 重复条目 '0' 表示键 'PRIMARY'

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

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

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

    【讨论】:

    • 是的。当在主键上禁用自动增量时会出现问题。
    【解决方案4】:

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

    【讨论】:

    • 嗨 Joshua,我想我可以补充一下(如果你还没有检查过)我遇到了一个非常相似的问题,结果我的 catalog_product_entity_int 表达到了 @ 的最大值987654322@ 类型,因此当我尝试创建一个新的简单产品时会做一些有趣的事情。我知道我正在研究的是Magento 1,但原理是一样的,也许问题是底层数据类型约束。
    【解决方案5】:

    我在使用 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-&gt;id=$request-&gt;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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2013-09-28
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      相关资源
      最近更新 更多