2013-10-14 228 views
11

我有一個databse問題,我得到完整性約束違規:1062. 我嘗試了一些我自己的東西,但它沒有工作,所以現在我問你大家看看你們是否可以幫助我。完整性約束違規:1062重複項'1'爲'PRIMARY'關鍵

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重複條目「1」鍵「PRIMARY」 中已經有一個主1鍵那裏。但它是在自動增量。在tekst tabel中,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

除非您向我們展示相關表格的創建聲明,否則我們無法爲您提供幫助。 –

+1

你有三個插入。你能分辨哪一個失敗? – asantaballa

+1

您是否試圖將值插入主鍵?如果是這樣 - 不要(在tekst tabel中,item_id獲得0輸入。) - >同時發佈你的表定義 – AgRizzo

回答

11

當插入帶有自動增量字段的表格時,自動增量字段本身不應該被指定。

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

應該只是

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

剛剛添加的答案從評論的討論,以便接受和完成的問題。

1

我有同樣的問題,它不是導致它的自動增量。我將我的表ID的數據類型從TINYINT(3)更改爲INT(10)。試試看。也許它會有所幫助。

+0

只是爲了給這個答案一些驗證,這是我的問題。我的表有'auto_increment'設置,我沒有指定主鍵,但我仍然收到錯誤。看到我的字段類型被設置爲'tinyint'而不是'int'。這個答案幫助了我,謝謝! – ckpepper02

0

我使用的Magento 2與谷歌實驗設置爲Yes時,碰到這個問題就來了。簡單地關閉它解決了我的頁面保存問題。如果在添加產品目錄產品時仍然存在問題,它會給我一個指定商店的URL密鑰已存在的錯誤。即使我擁有正確的文件夾權限,圖像也不會上傳。將發佈更新以防其他人幫助。

+0

嗨約書亞, 只是想我可以添加(如果你還沒有檢查)我得到了一個非常類似的問題,事實證明,我的'catalog_product_entity_int'表達到了'int'類型的最大值,因此做當我試圖創建一個新的簡單產品時,有趣的事情。我明白我的工作是'Magento 1',但原理是一樣的,也許問題是基礎數據類型約束。 –

相關問題