Comment system with foreign key "autor_coment"

Asked

Viewed 58 times

0

Has a comment table with the following attributes: id (int not null), comment (text not null), data_coment (timestamp not null) and autor_coment (int not null).

The autor_coment is a FK referencing another BD table (login).

I’m having difficulty to register the comment, and:

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

inserir a descrição da imagem aqui

  • 1

    What exactly are you having trouble with? Put the code of what you have done so far.

  • 1

    Sql Server or Mysql? Show an error? post the Insert command you are doing

1 answer

0

This is the registration code of the comment:

   <?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 register.

Browser other questions tagged

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