Insert data into related tables

Asked

Viewed 200 times

0

I’m making a small bulletin board with the option of replies to the comments.

I managed to do a lot of code, but as I have been away from programming for more than 10 years (my focus today is Linux servers and network infrastructure) I am having difficulties.

I can post the comment, the queries, both to extract the comments and to extract the answers, but 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, like I said, all right.

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

That’s the problem, how to get the exact id_comentario in the onclick of the specific comment?

Follows 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';
                                        }
                             }
                         }
                ?>  
  • Hehe...http://www.w3schools.com/php/php_mysql_insert_lastid.asp

  • 1

    @Magichat mysql_* functions are obsolete

  • 1

    @Take a look at this post http://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted#10680965

  • @Marcosxavier I know, this link was the first idea that came to mind, pq has some functions of db, but has myqli tmb...http://php.net/manual/en/mysqli.insert-id.php

  • But in case Pdo is here: http://php.net/manual/en/pdo.lastinsertid.php

No answers

Browser other questions tagged

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