How to make an INSERT inside the while

Asked

Viewed 394 times

0

Hello,

How do I insert into a loop where id of the comment = id of the comment look at the code

    $selecionarComentarios = $conexao->prepare("SELECT a.id_mark, a.id_user, a.comment, a.rate, a.id, a.active, b.name, b.avatar FROM tb_comment a, users b WHERE a.id_user=b.id AND a.id_mark = :post_id ORDER BY id DESC LIMIT $start_pg, $amount_pg");
    $selecionarComentarios->bindParam(':post_id',$post_id, PDO::PARAM_INT);
    $selecionarComentarios->execute();

    if($selecionarComentarios->rowCount() > 0)
    {

        while ($show = $selecionarComentarios->FETCH(PDO::FETCH_OBJ))
        {
            $rating_user               = $show->rate;
            $idComment                   = $show->id;
            $dataComentario          = $show->active;
            $timestamp                   = strtotime($dataComentario);
            $avatarUser                  = $show->avatar;
            $idUsuarioComentario = $show->id_user;

            if (empty($avatarUser))
          {
            $avatarUser = "default/default.jpg";
          }

            $selecionarVotos = $conexao->prepare("SELECT b.id_user, b.id_mark, b.id, a.id_mark, a.id_comment, COUNT(a.vote) as votos FROM tb_comment_ud a, tb_comment b WHERE a.id_user=b.id_user AND a.id_mark = b.id_mark AND a.id_comment=:idComment");
            $selecionarVotos->bindParam(':idComment',$idComment, PDO::PARAM_INT);
            $selecionarVotos->execute();

            if($selecionarVotos->rowCount() >= 1)
            {
                $mostrar = $selecionarVotos->fetch(PDO::FETCH_ASSOC);
                $votos = $mostrar['votos'];
            }

            if(isset($_POST['comentarioLike']))
            {
                $inserirLikeComentario = $conexao->prepare("INSERT INTO tb_comment_ud (id_user, id_mark, id_comment, vote, id_user_voting) VALUES (:idUsuarioComentario, :post_id, :idComment, 1, :idLogged)");
                $inserirLikeComentario->bindParam(':idUsuarioComentario',$idUsuarioComentario, PDO::PARAM_INT);
                $inserirLikeComentario->bindParam(':post_id',$post_id, PDO::PARAM_INT);
                $inserirLikeComentario->bindParam(':idComment',$idComment, PDO::PARAM_INT);
                $inserirLikeComentario->bindParam(':idLogged',$idLogged, PDO::PARAM_INT);

                $procurarVotos = $conexao->prepare("SELECT b.id_user, b.id_mark, b.id, a.id_user, a.id_mark, a.id_comment, a.id_user_voting FROM tb_comment_ud a, tb_comment b
                    WHERE a.id_user=b.id_user AND a.id_mark = b.id_mark AND b.id=:idComment AND a.id_user_voting=:idLogged");
                $procurarVotos->bindParam(':idComment',$idComment, PDO::PARAM_INT);
                $procurarVotos->bindParam(':idLogged',$idLogged, PDO::PARAM_INT);
                $procurarVotos->execute();

                if($procurarVotos->rowCount() >= 1)
                {
                    echo "Voce ja votou";
                }
                else
                {
                    $inserirLikeComentario->execute();
                }
            }

This code works, it inserts everything right only it inserts several records, it inserts the amount of records you have in SELECT $select

This one is the code that’s inside the loop:

<form action="#" method="post" enctype="multipart/form-data" class="form-horizontal" autocomplete="on">
                    <button type="submit" name="comentarioLike" class="btn btn-secondary" aria-label="Útil"><span class="icon-thumbs-up"></span></button>
                    </form>

My goal is this one, insert 1 record for each user who click on the like button that has in each comment.
inserir a descrição da imagem aqui

  • I’m not on the computer now to analyze the code as I understand it, if the Insert is already working the only thing to do is filter the comments by Id in your select. Or, only do Insert if the comment ID is equal to the id you sent in the post. But the right one would be to insert the like before starting to read the comments again.

  • Managed to solve William?

  • @luigibertaco Yes, more as I do that I tried already and could not.. I tried for WHERE on SELECT..

  • @durtto not yet.

  • @luigibertaco If I put the like before I start reading the comments the Insert will always for the first comment

  • @luigibertaco I’m letting something go.. ctz D=

Show 1 more comment

1 answer

0


Solved!!

I just swapped the _POST for the ID of each comment!!

$idComment = id de cada comentário

if(isset($_POST[$idComment]))
  • Mark your answer as accepted then...

Browser other questions tagged

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