Error mysql Duplicate entry ... for key "UNIQUE"

Asked

Viewed 205 times

0

Good Afternoon, I’m having trouble updating and inserting data into my mysql database, I’m making a php application but I’m not able to perform these update and Insert actions , follows the code image :

$qry_mysqli = "SELECT
                    g.id_grupo,
                    g.descricao_grupo,
                CASE
                WHEN b.id_grupo IS NULL THEN 0
                AND g.descricao_grupo IS NOT NULL
                ELSE 1
                END status
                FROM
                    grupo g
                LEFT JOIN base_conhecimento_grupo b ON (
                            b.id_grupo = g.id_grupo
                AND b.id_base = $id
                AND b.data_baixa IS NULL
                )
                WHERE
                    g.ativo = 1
                    AND g.id_grupo IN ('".implode('\',\'',$lista_artigo)."')";


    //echo $qry_mysqli;
    //mysqli_rollback($conexao);
    //return;
                         
$result = mysqli_query($conexao,$qry_mysqli);

    //echo $qry_mysqli;
    //mysqli_rollback($conexao);
    //return;            

  

    while($row = mysqli_fetch_object($result)){

        if ($row->status == 1){ // checa para ver se o status está ativo com o valor 1
                    $qry_update = "UPDATE base_conhecimento_grupo 
                                    SET id_grupo = '$row->id_grupo' , data_cadastro= sysdate() 
                                    WHERE id_base = $id ";
        echo $qry_update;
        mysqli_rollback($conexao);
        
           
        mysqli_query($conexao , $qry_update) or die (mysqli_error($conexao));
    
     }
    
    else{    //Realiza o incremento das informações no banco
        return;

                    $qry_inserir="  INSERT INTO 
                                    base_conhecimento_grupo (id_base,id_grupo,status,data_cadastro,id_usuario)
                                    VALUES
                                    ('$id','$row->id_grupo','1',sysdate(),'$id_usuario')";
                                    //echo $qry_inserir;
                                   // mysqli_rollback($conexao);                  
    }
                      mysqli_query($conexao,$qry_inserir) or die (mysqli_error($conexao));  
                   //  return;
                    
    }

When I test by the console appears this message:

UPDATE base_knowledge_group SET id_group = 'ADMTI' , data_cadastro= sysdate() WHERE id_base = 2 Duplicate entry '2-ADMTI' for key 'UNIQUE'

and

Duplicate entry '2-ADMLG' for key 'UNIQUE

  • 1

    the message is very clear, is inserting duplicate content ('2-ADMLG') in a field that is marked as 'UNIQUE'

  • I know, I was wondering what I have to change in the code to make her not insert the duplicate value

  • or you change the q comes from the variable or change your database by removing the UNIQUE option from the field in question

  • need to remove the field, can show the table structure? now if it is a key field will not give to insert duplicates

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.