SQLSTATE [23000]:完整性约束违规:1048列“消息”不能为空

时间:2017-06-23 21:35:56

标签: php html

我正在尝试用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>

任何人都知道如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

您的if语句(检查表单POST时)后跟另一个花括号分隔的块。将此块中的代码(SQL插入内容)移动到上面的大括号中(包含回声的那个&#34;&#34;)。

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();
            }
        }