Foreign key comment system "autor_coment"

0

It has a comments table with the following attributes: id ( int not null ), comment ( text not null ), data_coment ( timestamp not null ) and author_coment ( int not null ). > The author_coment is a FK that references another DB table ( login ).

I'm having trouble registering the comment, where:

  • I need to check if the user is logged in, otherwise he can not comment.
  • I need to show the name of the author_coment and not the id

    
asked by anonymous 12.04.2018 / 16:14

1 answer

0

This is the comment registration code:

   <?php
include 'admin/conexao/conecta.php';

if(isset($_POST['comentar'])){
            $comentario = trim(strip_tags($_POST['comentario']));

$insert = "INSERT into comentarios (comentario) VALUES (:comentario)";

        try{
            $result = $conexao->prepare($insert);
            $result->bindParam(':comentario', $comentario, PDO::PARAM_STR);
            $result->execute();
            $contar = $result->rowCount();
            if($contar>0){
                echo '';
            }else{
                echo '<script> alert("Erro ao comentar.") </script>';
            }           
        }catch(PDOException $e){
            echo $e;
        }

?>

<br>
<form method="post" action="#"> 
<table>
    <tr>
        <td>
            <b>Nome:</b> <br><input style="width: 200px;height: 40px;" type="text" id="autor_coment" name="autor_coment" placeholder="Digite seu nome">
        </td>
    </tr>
    <tr>
        <td style="padding-top: 10px;">
            <b>Comentário</b> <br>
            <textarea style="width: 500px; height: 150px;" type="text" id="comentario" name="comentario" required></textarea>
        </td>
    </tr>
    <tr>
        <td style="padding-left: 295px;">
        <input class="w3-button w3-black w3-section" style="width:100px; height:40px; background:#333; color:white; border:none;" type="reset" name="cancelar" value="Cancelar"/>
        <input class="w3-button w3-black w3-section" style="width:100px; height:40px; background:#333; color:white; border:none;" type="submit" name="comentar" value="Comentar"/>
        </td>
    </tr>
</table>
</form>
<br>

The problem is that I have no idea how to work with FK autor_coment in the registry.

    
13.04.2018 / 03:23