0

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> ";

?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
xo.
  • 485
  • 3
  • 10
  • In your forum.php snippet, you have two closing tags. Try removing the second one and see if it fixes the problem. – Guillermo Phillips Apr 14 '20 at 09:31
  • You have quite a peculiar idea on what an HTML form action is. It should be an url, not some chunk of PHP code. – Your Common Sense Apr 14 '20 at 09:32
  • @YourCommonSense I did find that weird, just following a tutorial online though to get a feel for how I can adapt. – xo. Apr 14 '20 at 09:33
  • @GuillermoPhillips Thank you for pointing out, still did not fix though :) – xo. Apr 14 '20 at 09:34
  • 1
    The browser should normally ignore mismatching closing tags, but it's good form to have matching opening and closing tags, and it can stop weird bugs. BTW edit-comments.php has the same issue. – Guillermo Phillips Apr 14 '20 at 10:03

1 Answers1

1

You have given input name = text instead of message in the below line in comments.php

 <input type='hidden' name='text' value='".$row['message']."'>

The correct code is

 <input type='hidden' name='message' value='".$row['message']."'>
Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26