Forum limit of votes by post PHP

1

Hi, I'm trying to make a forum in which registered users can write comments and assess, the problem is that a registered user can rate as many times as they want and I would like the user to rate 1 time per comment.

The page is link

The code is the following:

<script>

    function like(id) {
        if (id.length == 0) { 
            return;
        } else {
            window.location.href = "like.php?id="+id;
        }
    }

   function dislike(id) {
    if (id.length == 0) { 
        return;
    } else {
        window.location.href = "dislike.php?id="+id;
    }   
   }
    function deletecom(id)
    {
      if (id.length == 0) 
      {
         return;

      } else 
      {   

        alertify.confirm('Eliminar este comentario','¿Estas seguro que quieres eliminar este comentario?',
          function(){

            window.location.href = "deletecom.php?id="+id;
          },
          function(){
            alertify.error('Cancel');
          });
      }    
    }
    function añadir(){
        alertify.success('Ok');
    }

</script>
                    
asked by Trackless 12.06.2017 в 17:18
source

1 answer

1

To be able to control the votes of the users, you should create a table in the Database where before you have changed the vote, first validate if that user has already voted before, Example ..

session_start();

if(isset($_SESSION['usuario'])) {
$id = $_GET['id'];

realiza una consulta  select * from controlvotantes where idusuario=$id

if(si el usuario esta no esta en la tabla controlvotantes){ 

$conn = new mysqli('localhost','root','patata','radiohumorfm');
$like = "update comentarios set puntuacion=puntuacion+1 where id='$id'";
$conn->query($like);

insertar el usuario en la tabla controlvotantes 

insert into controlvotantes values ('$id')

ya si vuelve a votar el mismo usuario quedara en el primer filtro y no podra volver a votar 

$conn->close();   
header('Location: '.$_SERVER['HTTP_REFERER']);

}else{

si el usuario ya esta en la tabla controlvotantes es por que ya voto una vez 

   echo "Ya se a relizado una votacion con su usuario ";

}
}
else{

    echo "Acceso no permitido";
}
    
answered by 12.06.2017 / 18:12
source