-1

Hey I'm trying to make a comments section for a specific post on a php project im working on following this tutorial. https://www.youtube.com/watch?v=ckxlnISsa88 I can't find anything that will help. All it is doing is redirecting me to the action page but not submitting anything.

show-post.php:

   <?php

require 'config.php';
include 'templates/header.php';

$id = isset($_GET['id']) ? $_GET['id'] : '';

$query = $pdo->prepare( "SELECT * FROM posts WHERE id = ?");
$query->execute(array($id));
$data = $query->fetchAll(PDO::FETCH_ASSOC);




?>
<div class="container">
<?php foreach($data as $row) :?>

   <h2><?= $row['title'];?></h2>
   <?= '<img src="/createpost/images/'.$row['image'].'" alt="">' ?>
<?php endforeach ?>


<form  method="POST" id="comment_form" action="add_comment.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $id; ?>">
   <div class="form-group">
       <textarea name="comment" cols="30" rows="10"></textarea>
   </div>
   <div class="form-group">
       <input type="submit" name="submitcomment" id="submit" class="btn-success btn" value="Add Comment">
   </div>
</form>
<span id="comment_message"></span>
<br />
<div class="display_comment"></div>
<a href="/browse.php">Go Back</a>
</div>



 </php include "templates/footer.php"; ?>

add_comment.php

<?php
session_start();
require 'config.php';

include 'templates/header.php';

if(!isset($_SESSION['username'])){
    header("location:login.php");
} else{
    if(isset($_POST['submitcomment'])){
        $userid = $_SESSION['id'];
        $postid = $_POST['id'];
        $username = $_SESSION['username'];
        $comment = $_POST['comment'];

        if($comment != ""){

                    $query = "INSERT INTO comments (user_id, post_id, username, comment) VALUES(:userid, :postid, :username, :comment)";

                $stmt = $pdo->prepare($query);
                $stmt->execute(['user_id' => $userid,'postid' => $postid, 'username'=>$username, 'comment'=>$comment]);
                if($stmt){
                    header("location:show-post?id=".$postid);
                }


        }

    }
}

UPDATE: im getting these errors Notice: Undefined index: id in C:\Users\Zak\Documents\Code\crudapp\add_comment.php on line 13

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null in

  • try $stmt->error or mysqli_error($connection)) after the execute and prepare statement and show us the error that you got while submitting the form. Check how to get mysqli error on https://www.w3schools.com/php/func_mysqli_error.asp – Amanjot Kaur Dec 12 '19 at 06:46
  • I added $stmt->error; after the execute and im still not getting an error statement either... im using pdo so wouldn't a mysli function not work? – zturner2020 Dec 13 '19 at 02:26
  • Notice: Undefined index: id in C:\Users\Zak\Documents\Code\crudapp\add_comment.php on line 13 Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null in im getting these errors – zturner2020 Dec 13 '19 at 02:31
  • This means that the user_id field is sometimes getting null or empty values ... You can print the values before sending to MySQL query and check for which condition your user_id variable is getting empty values – Amanjot Kaur Dec 13 '19 at 05:09

1 Answers1

-1

I got it to submit by deleting the userid. The userid was returning null which i will look into why. Also I'm not sure why the tutorial needed the user id when the comment already gets the logged in username. Thanks for the help guys.