2017-06-23 10 views
0

私はPHPとHTMLでフォーラムを作ろうとしています。とにかく、これは正常に動作するために使用されていましたが、今ではこのエラーが発生しました。私はそれを修正する方法は分かりません。おそらくちょっとしたことです。SQLSTATE [23000]:整合性制約違反:1048カラム 'message'をnullにすることはできません。

<?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> 

これを修正する方法を知っている人はいますか?すでにありがとう!

答えて

0

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(); 
      } 
     } 
関連する問題