Like/Dislike php

Résolu/Fermé
Satanas_9168 Messages postés 18 Date d'inscription vendredi 18 décembre 2020 Statut Membre Dernière intervention 21 octobre 2021 - Modifié le 27 janv. 2021 à 14:14
Satanas_9168 Messages postés 18 Date d'inscription vendredi 18 décembre 2020 Statut Membre Dernière intervention 21 octobre 2021 - 27 janv. 2021 à 15:22
Bonjour voici mon code, j'ai fais un système like/dislike mais le problème c'est qu'une seule personne peut voter :
En gros si je like et qu'une autre personne vient sur mon site, ce sera comme si il avait liké à ma place. J'aimerais savoir comment permettre à tout le monde de voter et non une seule personne.

Merci !

Index.php :

<?php include('server.php'); ?>
<?phpsession_start();?>
<div class="posts-wrapper">
   <?php foreach ($posts as $post): ?>
    <div class="post">
      <?php echo $post['text']; ?>
      <div class="post-info">
     <!-- if user likes post, style button differently -->
       <i <?php if (userLiked($post['id'])): ?>
          class="fa fa-thumbs-up like-btn"
         <?php else: ?>
          class="fa fa-thumbs-o-up like-btn"
         <?php endif ?>
         data-id="<?php echo $post['id'] ?>"></i>
       <span class="likes"><?php echo getLikes($post['id']); ?></span>
       
           

     <!-- if user dislikes post, style button differently -->
       <i 
         <?php if (userDisliked($post['id'])): ?>
          class="fa fa-thumbs-down dislike-btn"
         <?php else: ?>
          class="fa fa-thumbs-o-down dislike-btn"
         <?php endif ?>
         data-id="<?php echo $post['id'] ?>"></i>
       <span class="dislikes"><?php echo getDislikes($post['id']); ?></span>
      </div>
    </div>
   <?php endforeach ?>
  </div>
  <script src="scripts.js"></script>


Scripts.js :

$(document).ready(function(){

// if the user clicks on the like button ...
$('.like-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-thumbs-o-up')) {
   action = 'like';
  } else if($clicked_btn.hasClass('fa-thumbs-up')){
   action = 'unlike';
  }
  $.ajax({
   url: 'index.php',
   type: 'post',
   data: {
    'action': action,
    'post_id': post_id
   },
   success: function(data){
    res = JSON.parse(data);
    if (action == "like") {
     $clicked_btn.removeClass('fa-thumbs-o-up');
     $clicked_btn.addClass('fa-thumbs-up');
    } else if(action == "unlike") {
     $clicked_btn.removeClass('fa-thumbs-up');
     $clicked_btn.addClass('fa-thumbs-o-up');
    }
    // display the number of likes and dislikes
    $clicked_btn.siblings('span.likes').text(res.likes);
    $clicked_btn.siblings('span.dislikes').text(res.dislikes);

    // change button styling of the other button if user is reacting the second time to post
    $clicked_btn.siblings('i.fa-thumbs-down').removeClass('fa-thumbs-down').addClass('fa-thumbs-o-down');
   }
  });  

});

// if the user clicks on the dislike button ...
$('.dislike-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-thumbs-o-down')) {
   action = 'dislike';
  } else if($clicked_btn.hasClass('fa-thumbs-down')){
   action = 'undislike';
  }
  $.ajax({
   url: 'index.php',
   type: 'post',
   data: {
    'action': action,
    'post_id': post_id
   },
   success: function(data){
    res = JSON.parse(data);
    if (action == "dislike") {
     $clicked_btn.removeClass('fa-thumbs-o-down');
     $clicked_btn.addClass('fa-thumbs-down');
    } else if(action == "undislike") {
     $clicked_btn.removeClass('fa-thumbs-down');
     $clicked_btn.addClass('fa-thumbs-o-down');
    }
    // display the number of likes and dislikes
    $clicked_btn.siblings('span.likes').text(res.likes);
    $clicked_btn.siblings('span.dislikes').text(res.dislikes);
    
    // change button styling of the other button if user is reacting the second time to post
    $clicked_btn.siblings('i.fa-thumbs-up').removeClass('fa-thumbs-up').addClass('fa-thumbs-o-up');
   }
  }); 

});

});


Server.php :

<?php 
// connect to database
$conn = mysqli_connect('localhost', 'Caché', 'Caché', 'Caché');

// lets assume a user is logged in with id $user_id
$user_id = 2;

if (!$conn) {
  die("Error connecting to database: " . mysqli_connect_error($conn));
  exit();
}

// if user clicks like or dislike button
if (isset($_POST['action'])) {
  $post_id = $_POST['post_id'];
  $action = $_POST['action'];
  switch ($action) {
   case 'like':
         $sql="INSERT INTO rating_info (user_id, post_id, rating_action) 
             VALUES ($user_id, $post_id, 'like') 
             ON DUPLICATE KEY UPDATE rating_action='like'";
         break;
   case 'dislike':
          $sql="INSERT INTO rating_info (user_id, post_id, rating_action) 
               VALUES ($user_id, $post_id, 'dislike') 
             ON DUPLICATE KEY UPDATE rating_action='dislike'";
         break;
   case 'unlike':
       $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
       break;
   case 'undislike':
         $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
      break;
   default:
    break;
  }

  // execute query to effect changes in the database ...
  mysqli_query($conn, $sql);
  echo getRating($post_id);
  exit(0);
}

// Get total number of likes for a particular post
function getLikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info 
      WHERE post_id = $id AND rating_action='like'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

// Get total number of dislikes for a particular post
function getDislikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info 
      WHERE post_id = $id AND rating_action='dislike'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

// Get total number of likes and dislikes for a particular post
function getRating($id)
{
  global $conn;
  $rating = array();
  $likes_query = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='like'";
  $dislikes_query = "SELECT COUNT(*) FROM rating_info 
       WHERE post_id = $id AND rating_action='dislike'";
  $likes_rs = mysqli_query($conn, $likes_query);
  $dislikes_rs = mysqli_query($conn, $dislikes_query);
  $likes = mysqli_fetch_array($likes_rs);
  $dislikes = mysqli_fetch_array($dislikes_rs);
  $rating = [
   'likes' => $likes[0],
   'dislikes' => $dislikes[0]
  ];
  return json_encode($rating);
}

// Check if user already likes post or not
function userLiked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id 
      AND post_id=$post_id AND rating_action='like'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
   return true;
  }else{
   return false;
  }
}

// Check if user already dislikes post or not
function userDisliked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id 
      AND post_id=$post_id AND rating_action='dislike'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
   return true;
  }else{
   return false;
  }
}

$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
// fetch all posts from database
// return them as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);


EDIT : Correction des balises de code : Ajout du LANGAGE
A voir également:

1 réponse

yg_be Messages postés 23327 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 15 novembre 2024 Ambassadeur 1 551
27 janv. 2021 à 14:19
bonjour,
ceci me fait penser à une tentative de sabotage:
// lets assume a user is logged in with id $user_id
$user_id = 2;
0
Satanas_9168 Messages postés 18 Date d'inscription vendredi 18 décembre 2020 Statut Membre Dernière intervention 21 octobre 2021
27 janv. 2021 à 14:23
??? comment ça ?
0
Satanas_9168 Messages postés 18 Date d'inscription vendredi 18 décembre 2020 Statut Membre Dernière intervention 21 octobre 2021
27 janv. 2021 à 14:33
Ha ne faudrait t'il pas que je remplace le deux par ma valeur sql ? celle de ma db.
0
Satanas_9168 Messages postés 18 Date d'inscription vendredi 18 décembre 2020 Statut Membre Dernière intervention 21 octobre 2021
27 janv. 2021 à 15:22
Ho je suis si stupid merci je devais juste mettre $id à la place du deux merci.
0