Help with form within WHILE

Asked

Viewed 17 times

-2

I would like to know how to get individual values of each like button, because when I like once a comment it ends up receiving the amount of others... Example; If you have 2 comments you get 2Likes, if 3 gets 3 and so on...

Follows the code

<?php
                          $selecionaLike = "SELECT * FROM likes WHERE idcomentario = '$idComentarioLinha'";
                          $execSelecionaLike = mysqli_query($conexaoBanco, $selecionaLike);
                          $contaLike = mysqli_num_rows($execSelecionaLike);
                          $selecionaDeslike = "SELECT * FROM deslike WHERE idcomentario = '$idComentarioLinha'";
                          $execSelecionaDeslike = mysqli_query($conexaoBanco, $selecionaDeslike);
                          $contaDeslike = mysqli_num_rows($execSelecionaDeslike);
                        ?>                            

                        <div id="like" style="padding-right: 10px;">
                        <form method="POST" action="" style="display: flex;flex-direction: row;">
                          <button type="submit" name="like" value="<?php echo($idComentarioLinha);?>" style="background-color: #44bd32;padding: 3px;border-radius: 5px;color: #fff;margin-right: 5px;"><i class="fas fa-thumbs-up"></i> <?php echo "$contaLike";?></button>
                          <button type="submit" name="deslike" value="<?php echo($idComentarioLinha);?>" style="background-color: #eb2f06;padding: 3px;border-radius: 5px;color: #fff;margin-right: 5px;"><i class="fas fa-thumbs-down"></i> <?php echo "$contaDeslike";?></button>                       
                        </form>                      

                          <?php
                          if(isset($_POST['like'])){
                            $i++;
                            $like = $_POST['like'];
                            echo "$like";
                            var_dump($like);
                            $likes = "INSERT INTO likes (idcomentario) VALUES ('$like')";
                            $execlikes = mysqli_query($conexaoBanco, $likes);
                          }
                          if(isset($_POST['deslike'])){
                            $deslike = $_POST['deslike'];
                            $deslikes = "INSERT INTO deslike (idcomentario) VALUES ('$deslike')";
                            $exedeslikes = mysqli_query($conexaoBanco,$deslikes);
                          }
                          ?>

1 answer

-1

Already solved, just took that part from inside the while...

<?php
                      if(isset($_POST['like'])){
                        $i++;
                        $like = $_POST['like'];
                        echo "$like";
                        var_dump($like);
                        $likes = "INSERT INTO likes (idcomentario) VALUES ('$like')";
                        $execlikes = mysqli_query($conexaoBanco, $likes);
                      }
                      if(isset($_POST['deslike'])){
                        $deslike = $_POST['deslike'];
                        $deslikes = "INSERT INTO deslike (idcomentario) VALUES ('$deslike')";
                        $exedeslikes = mysqli_query($conexaoBanco,$deslikes);
                      }
                      ?>

Browser other questions tagged

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