How to avoid dual Insert with different mysql Primary key

Asked

Viewed 78 times

1

My problem is critical in the system for some reason it inserts 2 times, I have tried to simulate the problem giving several clicks but did not duplicate. In my php code I validate if the record exists before inserting but still happens.

ID |DATA |other iguis fields...
-----------------------------------------------------------------
153 |2018-07-16 22:36:48|
154 |2018-07-16 22:36:48|

Insert by front

if(isset($_POST["btnCadastrar"])){
        require_once '../../BO/PPV.php';
        $p = new PPV();
        $p->ORIND = $_POST["optOrIn"];
        $p->ORIND_DESC = $_POST["txtOrIn"];
        $p->TIM = $_POST["optToMg"];
        $p->TIM_DESC = $_POST["txtToMg"];
        $p->ESCU = $_POST["optEsP"];
        $p->ESCU_DESC = $_POST["txtEsP"];
        $p->TERC = $_POST["optTerIn"];
        $p->TERC_DESC = $_POST["txtTerIn"];
        $p->MISSA = $_POST["optMiSa"];
        $p->MISSA_DESC = $_POST["txtMiSa"];
        $p->DESCO = $_POST["optDesc"];
        $p->DESCO_DESC = $_POST["txtDesc"];
        $p->PONTO = $_POST["optPonto"];
        $p->PONTO_DESC = $_POST["txtPonto"];
        $p->FAMI = $_POST["optFami"];
        $p->FAMI_DESC = $_POST["txtFami"];
        $p->SOCIE = $_POST["optSoci"];
        $p->SOCIE_DESC = $_POST["txtSoci"];
        $p->MISSAM = $_POST["optMiMe"];
        $p->MISSAM_DESC = $_POST["txtMiMe"];
        $p->TERCOEQ = $_POST["optTerEq"];
        $p->TERCOEQ_DESC = $_POST["txtTerEq"];
        $p->FORM = $_POST["optReuFor"];
        $p->FORM_DESC = $_POST["txtReuFor"];
        $p->INFO = $_POST["optReuFor"];
        $p->INFO_DESC = $_POST["txtReuFor"];
        $p->EVENTOS = $_POST["optEventos"];
        $p->EVENTOS_DESC = $_POST["txtEventos"];
        $p->TEMA = $_POST["optEstuTem"];
        $p->TEMA_DESC = $_POST["txtEstuTem"];
        $p->SUJES = $_POST["txtJuje"];
        $p->CRITICA = $_POST["txtCrit"];
        $p->RESPOSTAS = $_POST["txtResposta"];
        $p->ANO = date("Y");
        $p->REUNAIAO = $_GET["r"];
        $p->ID_USUARIO = $_COOKIE["IDUSU"];
        $p->ID_EQUIPES = $_COOKIE["IDEQP"];
        $p->ID_SETOR = $_COOKIE["IDSET"];
        $msg = $p->CadastrarPPV();
        if($msg == "PPV Enviado com Sucesso!"){

echo "$(Function () {showNotification('Alert-Success', ".$msg."', 'bottom', 'center', 'Animated bounceIn', 'Animated bounceOut');});"; }Else{ echo "$(Function () {showNotification('Alert-Danger', ". $msg." ', 'bottom', 'center', 'Animated bounceIn', 'Animated bounceOut');});"; } }
HTML

<form method="POST" name="cadastro" role="form">
                            <h3>1- Como tenho vivido minha Oração.</h3>
                            <hr>
                                    <div class="form-group">
                                        <label>Oração Individual</label>
                                        <select class="form-control" name="optOrIn">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtOrIn" id="txtOrIn">
                                    </div>
                                   <div class="form-group">
                                        <label>Time Out e Magnificat</label>
                                        <select class="form-control" name="optToMg">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtToMg" id="txtToMg">
                                    </div>
                                    <div class="form-group">
                                        <label>Escuta e Meditação da Palavra</label>
                                        <select class="form-control" name="optEsP">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtEsP" id="txtEsP">
                                    </div>
                                    <div class="form-group">
                                        <label>Terço Individual</label>
                                        <select class="form-control" name="optTerIn">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtTerIn" id="txtTerIn">
                                    </div>
                                    <div class="form-group">
                                        <label>Missa e Participação nos Sacramentos</label>
                                        <select class="form-control" name="optMiSa">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtMiSa" id="txtMiSa">
                                    </div>

                                    <h3>2- Como tenho feito a Personalização.</h3>
                                    <p class="help-block">O esforço para não vivermos nos enganando, buscando um conhecimento mais verdadeiro de nós mesmos, a partir da vivência conforme o Evangelho.</p>
                                    <hr>

                                    <div class="form-group">
                                        <label>a) Descoberta de si mesmo</label>
                                        <select class="form-control" name="optDesc">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtDesc" id="txtDesc">
                                        <p class="help-block">Nesse mês, o que tem me afastado de Deus?</p>
                                    </div>
                                    <div class="form-group">
                                        <label>b) Ponto de Esforço</label>
                                        <select class="form-control" name="optPonto">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtPonto" id="txtPonto">
                                        <p class="help-block">O que tenho feito para me aproximar de Deus?</p>
                                    </div>

                                    <h3>3- Como tenho vivido minha Missão.</h3>
                                    <hr>

                                    <div class="form-group">
                                        <label>Em Família</label>
                                        <select class="form-control" name="optFami">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtFami" id="txtFami">
                                    </div>
                                    <div class="form-group">
                                        <label>Na Sociedade</label>
                                        <select class="form-control" name="optSoci">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtSoci" id="txtSoci">
                                    </div>

                                    <h3>4- Como tenho Participado da Vida das Equipes.</h3>
                                    <hr>

                                    <div class="form-group">
                                        <label>Missa Mensal</label>
                                        <select class="form-control" name="optMiMe">
                                            <option value="Não Houve">Não Houve</option>
                                            <option value="Não Fui">Não Fui</option>
                                            <option value="Fui">Fui</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtMiMe" id="txtMiMe">
                                    </div>
                                    <div class="form-group">
                                        <label>Terço em Equipe</label>
                                        <select class="form-control" name="optTerEq">
                                            <option value="Não Houve">Não Houve</option>
                                            <option value="Não Fui">Não Fui</option>
                                            <option value="Fui">Fui</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtTerEq" id="txtTerEq">
                                    </div>
                                    <div class="form-group">
                                        <label>Reunião Formal</label>
                                        <select class="form-control" name="optReuFor">
                                            <option value="Não Houve">Não Houve</option>
                                            <option value="Não Fui">Não Fui</option>
                                            <option value="Fui">Fui</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtReuFor" id="txtReuFor">
                                    </div>
                                    <div class="form-group">
                                        <label>Reunião Informal</label>
                                        <select class="form-control" name="optReuInf">
                                            <option value="Não Houve">Não Houve</option>
                                            <option value="Não Fui">Não Fui</option>
                                            <option value="Fui">Fui</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtReuInf" id="txtReuInf">
                                    </div>
                                    <div class="form-group">
                                        <label>Eventos EJNS</label>
                                        <select class="form-control" name="optEventos">
                                            <option value="Não Houve">Não Houve</option>
                                            <option value="Não Fui">Não Fui</option>
                                            <option value="Fui">Fui</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtEventos" id="txtEventos">
                                    </div>

                                    <h3>5- Estudo do Tema.</h3>
                                    <hr>

                                    <div class="form-group">
                                        <label>Em que o estudo do tema ajudou minha caminhada como equipista?</label>
                                        <select class="form-control" name="optEstuTem">
                                            <option value="Não Fiz">Não Fiz</option>
                                            <option value="Fraco">Fraco</option>
                                            <option value="Bom">Bom</option>
                                            <option value="Intenso">Intenso</option>
                                        </select>
                                        <input class="form-control" placeholder="(Opcional)" name="txtEstuTem" id="txtEstuTem">
                                    </div>

                                    <h3>6- Sugestões e Críticas.</h3>
                                    <p class="help-block">(Equipe de Base, Setor, Eventos, SN, Temário)</p>
                                    <hr>

                                    <div class="form-group">
                                        <label>Sugestões</label>
                                        <input class="form-control" placeholder="Sugestões" name="txtJuje" id="txtJuje">
                                        <p class="help-block">Como podemos melhorar?</p>
                                    </div>
                                    <div class="form-group">
                                        <label>Críticas</label>
                                        <input class="form-control" placeholder="Críticas" name="txtCrit" id="txtCrit">
                                        <p class="help-block">O que está ruim?</p>
                                    </div>

                                    <h3>7- Respostas do Temário.</h3>
                                    <hr>
                                    <div class="form-group">
                                        <label>Resposta</label>
                                        <textarea rows="10" class="form-control" placeholder="Resposta" name="txtResposta" id="txtResposta"></textarea>
                                        <p class="help-block">Resposta do temário</p>
                                    </div>

                                    <button type="submit" class="btn btn-outline btn-success" name="btnCadastrar" id="btnCadastrar">Enviar PPV</button>
                        </form>


Php function

function CadastrarPPV(){
    if($this->ValidaPPV()) {
        require_once "../../Conexao/conexao.php";
        $c = new conexao();
        try{
            $c->Abrir();
            $Procedure = "INSERT INTO PPV
               (ORIND
               ,ORIND_DESC
               ,TIM
               ,TIM_DESC
               ,ESCU
               ,ESCU_DESC
               ,TERC
               ,TERC_DESC
               ,MISSA
               ,MISSA_DESC
               ,DESCO
               ,DESCO_DESC
               ,PONTO
               ,PONTO_DESC
               ,FAMI
               ,FAMI_DESC
               ,SOCIE
               ,SOCIE_DESC
               ,MISSAM
               ,MISSAM_DESC
               ,TERCOEQ
               ,TERCOEQ_DESC
               ,FORM
               ,FORM_DESC
               ,INFO
               ,INFO_DESC
               ,EVENTOS
               ,EVENTOS_DESC
               ,TEMA
               ,TEMA_DESC
               ,SUJES
               ,CRITICA
               ,DT_CADASTRO
               ,RESPOSTAS
               ,ANO
               ,REUNAIAO
               ,OS
               ,ID_USUARIO
               ,ID_EQUIPES
               ,ID_SETOR)
         VALUES
               ('".addslashes($this->ORIND)."'
               ,'".addslashes($this->ORIND_DESC)."'
               ,'".addslashes($this->TIM)."'
               ,'".addslashes($this->TIM_DESC)."'
               ,'".addslashes($this->ESCU)."'
               ,'".addslashes($this->ESCU_DESC)."'
               ,'".addslashes($this->TERC)."'
               ,'".addslashes($this->TERC_DESC)."'
               ,'".addslashes($this->MISSA)."'
               ,'".addslashes($this->MISSA_DESC)."'
               ,'".addslashes($this->DESCO)."'
               ,'".addslashes($this->DESCO_DESC)."'
               ,'".addslashes($this->PONTO)."'
               ,'".addslashes($this->PONTO_DESC)."'
               ,'".addslashes($this->FAMI)."'
               ,'".addslashes($this->FAMI_DESC)."'
               ,'".addslashes($this->SOCIE)."'
               ,'".addslashes($this->SOCIE_DESC)."'
               ,'".addslashes($this->MISSAM)."'
               ,'".addslashes($this->MISSAM_DESC)."'
               ,'".addslashes($this->TERCOEQ)."'
               ,'".addslashes($this->TERCOEQ_DESC)."'
               ,'".addslashes($this->FORM)."'
               ,'".addslashes($this->FORM_DESC)."'
               ,'".addslashes($this->INFO)."'
               ,'".addslashes($this->INFO_DESC)."'
               ,'".addslashes($this->EVENTOS)."'
               ,'".addslashes($this->EVENTOS_DESC)."'
               ,'".addslashes($this->TEMA)."'
               ,'".addslashes($this->TEMA_DESC)."'
               ,'".addslashes($this->SUJES)."'
               ,'".addslashes($this->CRITICA)."'
               ,NOW()
               ,'".addslashes($this->RESPOSTAS)."'
               ,'".$this->ANO."'
               ,".$this->REUNAIAO."
               ,'WEB'
               ,".$this->ID_USUARIO."
               ,".$this->ID_EQUIPES."
               ,".$this->ID_SETOR.")";
            $c->ExecQuery($Procedure);
            return "PPV Enviado com Sucesso!";
        }catch (Exception $e){
            return "Erro ao Enviar PPV - ".$e->getMessage();
        }//finally{
        $c->Fechar();
        //}
    } else {
        return "PPV ja Cadastrado, atualize a pagina para mais informacoes!";
    }
}


function ValidaPPV(){
    $IDPPV = "";
    require_once "../../Conexao/conexao.php";
    $c = new conexao();
    try{
        $c->Abrir();
        $SQL = "SELECT ID FROM PPV
                WHERE ANO = '".date("Y")."' AND REUNAIAO = ".$this->REUNAIAO." AND ID_USUARIO = ".$this->ID_USUARIO;
        $c->ExecQuery($SQL);
        while($eqp = $c->FetchArray()){
            $IDPPV = $eqp["ID"];
        }

        if($IDPPV == ""){
            return true;
        }else{
            return false;
        }
    }catch (Exception $e){
        return false;
    }//finally{
    $c->Fechar();
    //}
}
  • Without code it is very difficult to identify your problem, put the front-end and back-end code.

  • I updated the form is kind of great

  • It’s a very specific problem, it happened long before I put if($this->Validappv()) { and I was servicing the bank and noticed another duplicate record, but the same person sent 2018-07-16 22:41:05 the next meeting and did not duplicate it.

1 answer

1


If you’re doing this validation and it’s not working, which in my view was supposed to work. I could try to make a unset($_POST["btnCadastrar"]) to ensure that this guy will no longer come filled by some browser cache, or for some other mysterious reason. It might solve.

  • 1

    I hadn’t thought about it, I added now it’s a matter of time, thank you

  • You are doing the form Ubmit via Ajax

  • Check if in any situation the $_GET["and"] parameter might not be filled and if it would not be better to put in the form as Hidden since it is a parameter used in your checking

Browser other questions tagged

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