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