Error inserting data in table, duplicate record

1

Greetings. I am new here, my problem is as follows. My script is making a duplicate insert to the bd after running for the first time.

Example: First insertion: well, insert the record once.

Second insertion (this is done after doing a .load () from ajax to the section in which I show the data): insert the same record twice.

Third insertion: insert 3 times the same record, and so on.

The functionality is that after inserting the comment, make a .load () to the comment section to see the new comment, and if you want to reinsert another one, let me do it, and here is the problem, after the first insert, make the other duplicate insert.

These are my functions:

AJAX:

jQuery(document).on('submit','#comment_form',function(event){
    event.preventDefault();
    if($("#comment").val()=="" || $("#n").val()==""){
        $("#alert").html('Fill the fields');
    }else if($("#comment").val().length > 300){
        $("#alert").html('We allow 300 characters maximum for a comment');
    }else if(!expr.test($("#n").val())){
        $("#alert").html('Write a correct name');
    } else {
        jQuery.ajax({
            data: $(this).serialize(),
            type: 'POST',
            dataType:'json',
            url:'core/bin/goComent.php',
            beforeSend: function(){
                $("#submit").attr("disabled","disabled");
                $("#submit").val("Sending");
            }
        })

        .done(function(json){
            console.log(json);
            if(json.success == true){
                setTimeout(function(){
                    $("#commentss").load("core/viewsComments.php").fadeIn('slow');
                },1000);
                $("#submit").removeAttr("disabled");
                $("#submit").val("Post Comment");
                $("#n").val("");
                $("#comment").val("");
                return false;
            } else {
                $("#alert").html(json);
            }
        })
    }
});

PHP:

<?php 
  if($_POST){
  $jsondata = array();
  $comment = strip_tags($_POST['comment']);
  $name = strip_tags($_POST['n']);
  $url = $_POST['url'];
   if(empty($url)){
     $url = $_SERVER["HTTP_HOST"];
   }
   if(!empty($name) or !empty($comment)){
     require('../clases/classConexion.php');
     include '../libs/geoIP/geoiploc.php';
     $db = new Conexion();
     $ip = $_SERVER["REMOTE_ADDR"];
     //$pais = getCountryFromIP($ip,'name');
     $sql = $db->prepare("INSERT INTO comentarios(nombre,comentario,ip,url,date_)VALUES(?,?,?,?,NOW())");
    if($sql->execute(array($name,$comment,$ip,$url))){
        $jsondata['success'] = true;
    } else {
        $jsondata['success'] = false;
        $jsondata['msj'] = $sql->errorInfo()[2];
    }
   } else {
    echo '<script>alert("Empty Data, NO");</script>';
   }
   } else {
     echo '<script>alert("You can not enter here");</script>';
   }
   header('Content-type: application/json; charset=utf-8');
   echo json_encode($jsondata);
   exit();
?>

I have not gotten the reason for this problem! I check and check and I do not give with the error.

    
asked by Ghyslaine Henriquez 10.04.2018 в 17:42
source

0 answers