为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
qq_花开花谢_0 2022-12-23 14:25:42
我正在学习如何向站点添加论坛部分的教程,用户可以在其中发表评论,然后使用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>             
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

您在 comments.php 的下面一行中给出了 input name=text而不是message

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

正确的代码是

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


查看完整回答
反对 回复 2022-12-23
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信