How to do an INSERT within the while

0

Hello,

How do I do an INSERT within a loopback where comment id = comment id looks 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 just as it inserts multiple records, it inserts the amount of records it has in SELECT $ selectComments

This is the code that is inside the loopback:

<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 here is to insert 1 record for each user that clicks on the like button they have in each comment.

    
asked by anonymous 13.01.2016 / 06:02

1 answer

0

Solved !!

I only changed _POST by the ID of each comment !!

$idComment = id de cada comentário

if(isset($_POST[$idComment]))
    
14.01.2016 / 03:40