繁体   English   中英

SQLSTATE [23000]:违反完整性约束:1048列“ message”不能为空

[英]SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'message' cannot be null

我正在尝试使用PHP和HTML创建一个论坛,但我仍在开始学习,所以我喜欢堆栈溢出来获取答案。 无论如何,这曾经可以正常工作,但现在我收到了这个错误。 我不知道如何解决它,这可能只是一件小事。

    <?php
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
    $user_home->redirect('index.php');
}

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <title>forum</title>
    <meta content="width=device-width, initial-scale=1.0" name="viewport" >
    <link rel="stylesheet" href="loginforum/style2/bb.css" type="text/css" />
</head>
<body>

<div>
    <img class="resize" src="">

    <p><input type="submit" value="start new post" href="starttopic.php">
        <a href="starttopic.php"> lol </a></p>

    <div id="main">
        <div class="form" style="width:300px; margin-right:130px; margin-left:auto; margin-bottom: 10px;">
            <form action="" method="post">
                <input type="submit" value = "Logout">
                <a href="logout.php?logout=true"><i class="glyphicon glyphicon-log-out"></i> logout</a>
            </form>
        </div>

        <div class="container">
            <div class="logo">
                <img src="loginforum/accountview/logo.png">
            </div>
            <p class="navbar-text" style="color: white;">welcome - <a href="profile.php" role="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="icon-user"></i>
                    <?php echo $row['userEmail']; ?> <i class="caret"></i></a>




                <?php
                $stmt = $user_home->runQuery("SELECT title, message, starter FROM topics WHERE id = :id");
                $stmt->execute([
                    ':id'=>$_GET['rowid'],
                ]);
                $rows = $stmt->fetchAll();
                echo '<pre>';
                print_r($rowid = $_GET['rowid']);
                $stmt = $user_home->runQuery("SELECT title, message, starter FROM topics WHERE ID=:ID");
                foreach ($rows as $row){;
                    $newtitle = wordwrap($row ['title'], 120);
                    echo '<span style = "color: #ff0000"> <h3> ' . $newtitle . ' </h3> </span>';
                    $newtext = wordwrap($row ['message'], 120, "<br />\n");
                    echo '<span style = "color: #ff0000"> ' . $newtext . '  </span>';

                }


                ?>

            <hr>

            <form method="post">
                <h2 style="color: white">Comments</h2>
                <?php
                $content = "";
                print " <textarea name='message' rows=\"4\" cols=\"50\" onkeyup=\"this.value = this.value.replace(/[&*<>]/g, '')\">". htmlentities($content) ."</textarea>";
                ?>
                <button action="window.location.reload()" name="sumbit" type="submit" id="ta" data-submit="...Sending">Submit</button>
            </form>


            <script>  var ta = document.getElementById("ta");
                ta.addEventListener(
                    'keypress',
                    function (e) {
                        // Test for the key codes you want to filter out.
                        if (e.keyCode == 60) {
                            alert('No "<"!');
                            // Prevent the default event action (adding the
                            // character to the textarea).
                            e.preventDefault();
                        }
                    }
                ); </script>

            <?php
            if($_SERVER['REQUEST_METHOD'] == 'POST')       {

                echo "<meta http-equiv='refresh' content='0'>";

            }{

                try {
                    $message = $_POST[ 'message' ];
                    $sql = "INSERT INTO replies ( 
            message, username, topicid ) 
            VALUES ( 
            :message, :username, :topicid )";
                    if($user_home->is_logged_in()) {
                        $username = $_SESSION['username'];
                        $topicid = $rowid = $_GET['rowid'];
                        $stmt = $user_home->runQuery($sql);
                        $stmt->execute( array(':message'=>$message, ':username' => $username, ':topicid'=>$topicid) );
                    };
                }
                catch(PDOException $e)
                {
                    echo $e->getMessage();
                }
            }

            $stmt = $user_home->runQuery("SELECT topicid, username, message FROM replies WHERE topicid = :idrow");
            $stmt->execute([
                ':idrow' =>$_GET['rowid']
            ]);
            $rowidc = $stmt->fetchAll();
            echo '<pre>';
            foreach ($rowidc as $rowz){
                $newtitless = wordwrap($rowz ['username'], 120);
                $newtitles = wordwrap($rowz ['message'], 120);
                echo '<hr>';
                echo '<h3 style = "color: #ff0000"> Reply by ' . $newtitless . ' </h3> ';
                echo '<h4 style = "color: #ff0000"> ' . $newtitles . ' </h4> ';
                echo '<hr>';}
            ?>
        </div>
    </div>
</div>
</body>
</html>

有人知道如何解决这个问题吗? 已经谢谢你了!

您的if语句(在检查表单POST时)后面是另一个花括号分隔的块。 将此块中的代码(SQL插入内容)移动到上面的花括号中(包裹回声“”的那个)。

if($_SERVER['REQUEST_METHOD'] == 'POST')       {

            echo "<meta http-equiv='refresh' content='0'>";

        // }{ # OR JUST REMOVE THIS LINE

            try {
                $message = $_POST[ 'message' ];
                $sql = "INSERT INTO replies ( 
        message, username, topicid ) 
        VALUES ( 
        :message, :username, :topicid )";
                if($user_home->is_logged_in()) {
                    $username = $_SESSION['username'];
                    $topicid = $rowid = $_GET['rowid'];
                    $stmt = $user_home->runQuery($sql);
                    $stmt->execute( array(':message'=>$message, ':username' => $username, ':topicid'=>$topicid) );
                };
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM