猿问

未定义的索引:PHP 脚本中的消息

我正在学习如何向站点添加论坛部分的教程,用户可以在其中发表评论,然后使用PHP和编辑它们MySQL。


在数据库中发布评论绝对没问题,但是当我点击编辑时,$message = $_POST['message'];当我在页面上(也不是在文本框中)回显时不会打印出来,并且我收到错误消息Undefined index: message。


请有人阐明我对未进入页面的消息的实际内容做错了什么edit-comments.php。


论坛.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);

    ?>

评论.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>

             

qq_花开花谢_0
浏览 96回答 1
1回答

慕森卡

您在 comments.php 的下面一行中给出了 input&nbsp;name=text而不是message&nbsp;<input&nbsp;type='hidden'&nbsp;name='text'&nbsp;value='".$row['message']."'>正确的代码是&nbsp;<input&nbsp;type='hidden'&nbsp;name='message'&nbsp;value='".$row['message']."'>
随时随地看视频慕课网APP
我要回答