Insert data into related tables

0

I'm doing a little scrapbook with the option to replies to the comments.

I was able to do a lot of the code, but since I'm away from programming for over 10 years (my focus today is Linux servers and network infrastructure) I'm having a hard time.

I can post the comment, the queries, both to extract the comments and to extract the answers, however I have no ideas to include the answers in the table.

The related tables are as follows:

create table if not exists tb_comentarios
    (
           id_comentario int not null auto_increment,
           titulo varchar (30) not null,
           nome varchar (50) not null,
           comentario varchar (255) not null,
           data_postagem varchar (20) not null,
           aprovo int (5)not null default 0,
           desaprovo int (5)not null default 0,
           ip_host varchar (50)not null,
           primary key (id_comentario),
           index (nome,data_postagem)
        )engine=innodb;

create table if not exists tb_respostas
    (
           id_resposta int not null auto_increment,
           id_comentario int (5)not null,
           nome varchar (50) not null,
           resposta varchar (255) not null,
           data_postagem varchar (20) not null,
           aprovo int (5)not null default 0,
           desaprovo int (5)not null default 0,
           ip_host varchar (50)not null,
           primary key (id_resposta),
           index (nome,data_postagem),
           foreign key (id_comentario) references tb_comentarios (id_comentario) on delete cascade on update cascade
    )engine=innodb;

The structure is as follows:

Comentário

Resposta 01 
.
.
.
Resposta N

So far, as I said, fine.

I created a second form to be used for sending responses, a second script for insertion, what I need now is to find a way to when I click on the reply link, get id_comentario and send it to the script. insertion of response as foreign key for table tb_respostas .

That's the problem, how do you get the exact id_comentario in the onclick of the specific comment?

Here's part of the code:

<div class="window" id="formulario_comentario">
                <a href="#" class="fechar link_mural_comentario">X Fechar</a>
                <p class="formatacao_padrao">
                <div id="status"></div>
                    <form name="formulario_comentario" action="comentario.php" method="post">
                        <fieldset> 
                        <label>titulo:</label> 
                        <br>
                            <input name="titulo" type="text" id="titulo" size="35" />                           
                            <br>
                            <label>nome:</label> 
                            <br>
                        <input name="nome" type="text" id="nome" size="35" />
                         <br>
                            <label>recado:</label> 
                            <br>
                            <textarea name="comentario" id="comentario" rows="5" cols="40"></textarea>
                         <br>
                         <br> 
                        <div align="center"><input class="botao_enviar" type="submit" value="Enviar" />
                        <input type="reset" value="Limpar"></div>
                   </fieldset>
                </form>
                </p>
            </div>              
                <?php
                //Inclui o arquivo de conexão com o banco de dados.
                include 'conexao.php';

                //Seleciona e exibi os dados da tabela tb_comentarios por ordem de data de postagem. 
                       $stmte01 = $PDO->prepare("select * from tb_comentarios order by id_comentario desc");
                       $executa = $stmte01->execute();
                       if($executa)
                        {
                           while($reg = $stmte01->fetch(PDO::FETCH_OBJ))
                                { /* Para recuperar um ARRAY utilize PDO::FETCH_ASSOC */
                                     echo "<p class='formatacao_padrao post_it_amarelo'>";
                                            echo "<img src='../img/post-it-taxinha.png' width='40' height='32' alt=''>";
                                            $id_comentario=$reg->id_comentario;                 
                                            echo "<b>".$reg->titulo."</b><br>";                 
                                        echo $reg->comentario ."<br>";
                                        echo "postado por : <b>".$reg->nome."</b> em ";
                                        echo $reg->data_postagem;"<br>";
                                        echo "<br><br>";
                                        echo "<a href='#formulario_resposta' rel    ='modal' class='link_resposta'>Responder</a>";
                                    echo "</p>";
                                    $stmte02 = $PDO->prepare("select * from tb_respostas where id_comentario= $id_comentario order by id_comentario desc");
                                     $executa = $stmte02->execute();
                                     if($executa)
                                        {
                                             while($reg=$stmte02->fetch(PDO::FETCH_OBJ)) 
                                                 {
                                                            echo "<p class='formatacao_padrao post_it_azul'>";
                                                                echo "<img src='../img/post-it-taxinha.png' width='40' height='32' alt=''>";                                    
                                                            echo $reg->resposta ."<br>";
                                                            echo "postado por : <b>".$reg->nome."</b> em ";
                                                            echo $reg->data_postagem;"<br>";
                                                            echo "<br><br>";
                                                       echo "</p>";

                                                 }
                                        }
                                       else
                                        {
                                           echo 'Erro ao exibir dados';
                                        }
                             }
                         }
                ?>  
    
asked by anonymous 07.12.2016 / 13:57

0 answers