How to add the value of a User_ID to an FK?

0

Hello ...

Well, assuming a user wants to comment on a post. The system should:

  • Verify that the user is logged in, otherwise he will receive a script "You must be logged in to comment".

  • If the user is logged in, then he should get the id of that user and move to autor_coment (which is a FK of the comments table)

    That's it. As I'm starting and I did not find any examples of codes like this, I decided to ask for help here. However, when registering the comment I can not leave autor_coment null, since it is a FK . So I wanted to get the id of the logged in user and move to autor_coment .

    In short ... when I post a comment I can not leave this to FK autor_coment null. So I want to pass the value of the user id logged in to this FK .

    Until then, I have this code to register the comments:

    <?php
    if(isset($_POST['comentar'])){
                $autor_coment       = trim(strip_tags($_POST['autor_coment']));
                $comentario             = trim(strip_tags($_POST['comentario']));
    
        if(empty($autor_coment)){
            echo "<script>alert('Preencha todos os campos para comentar.'); history.back();</script>";
        }elseif(empty($comentario)){
            echo "<script>alert('Preencha todos os campos para comentar.'); history.back();</script>";
        }else{
    
        $insert = "INSERT into comentarios (autor_coment, comentario) VALUES (:autor_coment, :comentario)";
    
            try{
                $result = $conexao->prepare($insert);
                $result->bindParam(':autor_coment', $autor_coment, PDO::PARAM_STR);
                $result->bindParam(':comentario', $comentario, PDO::PARAM_STR);
                $result->execute();
                $contar = $result->rowCount();
                if($contar>0){
                    echo "<script>alert('Seu comentário foi enviado com sucesso!')</script>";
                    header("Location: ../index.php");
                }else{
                    echo "<script>alert('Erro! Não foi possível comentar')</script>";
                }           
            }catch(PDOException $e){
                echo $e;
            }   
        }
    }
    ?>
    
    <br>
        <form action="" method="post" enctype="multipart/form-data">
    
                        <p>
    
                            <input 
                            style="width: 400px;" 
                            type="" 
                            name="id"
    
                            required
                            placeholder="ID">
    
                            <br><br>
    
                            <input 
                            style="width: 400px; height: 150px;"
                            type="text" 
                            id="comentario" 
                            name="comentario" 
                            value="" 
                            required
                            placeholder="Digite seu comentário"
                            /> 
    
    <br>
                        <a style="padding-left: 280px;">
                            <input 
                            style="width:120px; height:40px; background:#333; color:white; border:none;"
                            type="submit" 
                            name="comentar" 
                            class="w3-button w3-black w3-section"
                            value="Comentar"
                            />
                        </p></a>
    
        </form> 
    

    I'm getting the user ID in the comment and I'm not verifying that the user is logged in or not. So there are two problems that I have no idea how to solve.

        
  • asked by anonymous 17.04.2018 / 03:34

    0 answers