Hello everyone,
I am trying to submit a comment in a comment box and send it to the DB but is not happening. The connection is good as I am logging in and all but no data is sent to the DB when I post the comment. It doesn't show in my comment section either.
Form
<!--comment section-->
<?php
if(isset($_SESSION['id'])) {
echo "<form method='POST' action='" . setComments($conn) . "'>
<input type='hidden' name='uidUsers' value='".$_SESSION['id']."'>
<input type='hidden' name='posted' value='" . date('Y-m-d H:i:s') . "'>
Comments: <textarea rows = '5' cols = '15' name='body'></textarea><br><br>
<button name='commentSubmit' type='submit'>Comment</button>
</form>";
}else {
echo "Log in to comment!";
}
getComments($conn);
Function to set and get comments
function setComments($conn) {
if (isset($_POST['commentSubmit'])){
$user_id = $_POST['uidUsers'];
$body = $_POST['body'];
$posted = $_POST['posted'];
$sql = "INSERT INTO comments (uidUsers, posted, body) VALUES ('$user_id', '$posted', '$body')";
$result = mysqli_query($conn, $sql);
}
}
function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()){
$id = $row['uidUsers'];
$sql2 ="SELECT * FROM users WHERE uidUsers='$id'";
$result2 = mysqli_query($conn, $sql2);
if($row2 = $result2->fetch_assoc()){
echo "<div class='comment-box'><p>";
echo $row2['uidUsers'] . "<br>";
echo $row['posted'] . "<br>";
echo nl2br($row['body']);
echo "</p></div>";
}
}
}