What's new

Anu kaya mali sa code ko

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
ito po ang error

Warning: Undefined array key "comment_content" in C:\xampp\htdocs\Innerjoin\user\comment.php on line 93

<div class="container col-md-6 bg-body-tertiary border mt-3 rounded-3" >
<form method="POST">
<input type="hidden" name="id" value="<?php echo $username; ?>">
<label class="fw-bold mb-2" for="w3review">Comment:</label>
<textarea name="comment_content" class="form-control" id="post" rows="4" cols="40" placeholder="Your Content....."></textarea>
<div class="form-group mb-2 mt-2" style="text-align: right">
<input type="submit" name="comment" value="Reply" class="w-25" value="Post" style="background-color:#0D52C7;">
</div>
</form>
</div>
<?php

if (isset($_POST['comment'])){
$comment_content = $_POST['comment_content'];
$post_id=$_POST['id'];

mysqli_query($conn,"insert into comment (content,date_posted,user_id,post_id) values ('$comment_content','".strtotime(date("Y-m-d h:i:sa"))."','$user_id','$post_id')") or die (mysqli_error());
header('location:post.php');
}
?>
 
The error message you are receiving is stating that the array key "comment_content" is undefined on line 93 of your comment.php file.

Looking at your code, it seems that the form is being submitted via POST method and you are trying to access the "comment_content" value from the $_POST superglobal array.

The issue might be that the "comment_content" field is not being submitted or it is empty when the form is submitted.

To fix this error, you can add some validation to ensure that the "comment_content" field is not empty before accessing its value in the $_POST array.

Here's an example of how you can modify your code:

PHP:
<?php
if (isset($_POST['comment'])){
    $comment_content = isset($_POST['comment_content']) ? $_POST['comment_content'] : ''; // Check if the field is set and assign its value to $comment_content variable, otherwise assign an empty string
    $post_id = isset($_POST['id']) ? $_POST['id'] : ''; // Check if the field is set and assign its value to $post_id variable, otherwise assign an empty string

    if (!empty($comment_content) && !empty($post_id)) { // Check if both fields are not empty
        mysqli_query($conn, "insert into comment (content, date_posted, user_id, post_id) values ('$comment_content', '" . strtotime(date("Y-m-d h:i:sa")) . "', '$user_id', '$post_id')") or die(mysqli_error());
        header('location:post.php');
    } else {
        echo "Comment content or post ID is missing.";
    }
}
?>

With this modification, if the "comment_content" field is not submitted or empty, the script will display an error message instead of trying to access the undefined array key.
 

Similar threads

Back
Top