I am following a tutorial on how to add a forum section to the site, where users can post comments, and then edit them using PHP
and MySQL
.
Posting a comment into the DB works absolutely fine, however it's when I go to click on edit, the $message = $_POST['message'];
does not print out when I echo it on page (nor in the text box) and I get the error message of Undefined index: message
.
Please can someone shed some light on what I have done incorrectly for the actual content of the message not pulling through to the page edit-comments.php
.
forum.php:
<?php
require 'header.php';
date_default_timezone_set('Europe/London');
include 'comments.php';
$studentID = $_SESSION['studentID'];
echo "<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='studentID' value='".$studentID."'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea class='cmtBox' name='message'></textarea>
<br>
<button class='btnCmtBox' type='submit' name='cmt-submit'>Comment</button>
</textarea>
</form> ";
getComments($conn);
?>
comments.php:
<?php
function setComments($conn) {
if(isset($_POST['cmt-submit'])){
$message = $_POST['message'];
$date = $_POST['date'];
$studentID = $_POST['studentID'];
$sql = "INSERT INTO `comment` (`message`, `date`, `studentID`) VALUES ('$message', '$date', '$studentID') ";
$result = $conn->query($sql);
// $stmt = $conn->prepare ("INSERT INTO `comment` (`message`, `date`, `studentID`) VALUES (?, ?, ?) ");
// $stmt->bind_param("iii", $comment, $date, $username);
// $stmt->execute();
// $result = $stmt->get_result();
}
}
function getComments($conn) {
$sql = "SELECT `comment`.`message`, `comment`.`date`, `student`.`studentID`, `student`.`username` FROM `comment` INNER JOIN `student` ON `comment`.`studentID` = `student`.`studentID`";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<div class='cmt-box'><p>";
echo $row['studentID']."<br>";
echo $row['username']."<br>";
echo $row['date']."<br>";
echo nl2br($row['message']);
echo "</p>
<form class='edit-form' method='POST' action='edit-comment.php'>
<input type='hidden' name='studentID' value='".$row['studentID']."'>
<input type='hidden' name='username' value='".$row['username']."'>
<input type='hidden' name='date' value='".$row['date']."'>
<input type='hidden' name='text' value='".$row['message']."'>
<button class='btnEditCmt'>Edit</button>
</form>
</div>";
}
}
function editComments($conn) {
if(isset($_POST['cmt-submit'])){
$message = $_POST['message'];
$date = $_POST['date'];
$username = $_POST['username'];
$sql = "INSERT INTO `comment` (`message`, `date`, `studentID`) VALUES ('$message', '$date', '$username') ";
$result = $conn->query($sql);
}
}
?>
edit-comments.php:
<?php
require 'header.php';
date_default_timezone_set('Europe/London');
include 'comments.php';
$studentID = $_POST['studentID'];
$message = $_POST['message'];
$date = $_POST['date'];
$username = $_POST['username'];
echo $studentID."<br>";
echo $message."<br>"; // This is line 42 that holds the error msg
echo $date."<br>";
echo $username."<br>";
echo "<form method='POST' action='".editComments($conn)."'>
<input type='hidden' name='studentID' value='".$studentID."'>
<input type='hidden' name='username' value='".$username."'>
<input type='hidden' name='date' value='".$date."'>
<textarea class='cmtBox' name='message'>".$message."</textarea> <!-- Which means the post/message also does not pull through to here -->
<br>
<button class='btnCmtBox' type='submit' name='cmt-submit'>Edit</button>
</textarea>
</form> ";
?>