title: Prevent submitting if input field is empty
body: <p>I developing a comment system for my social media, but comments are inserted into database even if it's empty.So i want nothing to happen if comment field is empty,i don't want to echo any message or submitting the comment. </p>
  
  <p>This is my code</p>
  
  <pre><code>    &lt;?php 
      // Get id of post
      if(isset($_GET['post_id'])) {
  
          $post_id = $_GET['post_id'];
      }
  
      $user_query = mysqli_query($con, "SELECT added_by, user_to FROM posts 
      WHERE id='post_id'");
      $row = mysqli_fetch_array($user_query);
  
      $posted_to = $row['added_by'];
      $user_to = $row['user_to'];
  
      if(isset($_POST['postComment' . $post_id])) {
  
          $post_body = $_POST['post_body'];
          $post_body = mysqli_escape_string($con, $post_body);
          $date_time_now = date("Y-m-d H:i:s");
      $insert_post = mysqli_query($con, "INSERT INTO comments VALUES ('', 
      '$post_body', '$userLoggedIn', '$posted_to', '$date_time_now', 'no', 
      '$post_id')");
  
      if($posted_to != $userLoggedIn) {
  
          $notification = new Notification($con, $userLoggedIn);
          $notification-&gt;insertNotification($post_id, $posted_to, "comment");
      }
  
      if($user_to != 'none' &amp;&amp; $user_to != $userLoggedIn) {
  
          $notification = new Notification($con, $userLoggedIn);
          $notification-&gt;insertNotification($post_id, $user_to, 
          "profile_comment");
      }
  
      $get_commenters = mysqli_query($con, "SELECT * FROM comments WHERE 
      post_id='$post_id'");
      $notified_users = array();
      while($row = mysqli_fetch_array($get_commenters)) {
  
          if($row['posted_by'] != $posted_to &amp;&amp; $row['posted_by'] != $user_to
              &amp;&amp; $row['posted_by'] != $userLoggedIn &amp;&amp; 
          !in_array($row['posted_by'], $notified_users)) {
  
              $notification = new Notification($con, $userLoggedIn);
              $notification-&gt;insertNotification($post_id, $row['posted_by'], 
             "comment_non_owner");
  
              array_push($notified_users, $row['posted_by']);
  
          }
  
      }
  
  
  
      echo "&lt;p&gt;Comment Posted! &lt;/p&gt;";
      }
  
      ?&gt;
  
     &lt;form action="comment_frame.php?post_id=&lt;?php echo $post_id; ?&gt;" 
     id="comment_form" name="postComment&lt;?php echo $post_id; ?&gt;" 
      method="POST"&gt;
      &lt;textarea name="post_body" placeholder="Add a comment"&gt;&lt;/textarea&gt;
      &lt;input type="submit" name="postComment&lt;?php echo $post_id; ?&gt;" 
       value="Comment"&gt; 
  
   &lt;/form&gt;
  
   &lt;!-- Load Comments --&gt;
   &lt;?php 
   $get_comments = mysqli_query($con, "SELECT * FROM comments WHERE 
   post_id='$post_id' ORDER BY id ASC");
   $count = mysqli_num_rows($get_comments);
  
   if ($count != 0) {
  
      while($comment = mysqli_fetch_array($get_comments)) {
  
          $comment_body = $comment['post_body'];
          $posted_to = $comment['posted_to'];
          $posted_by = $comment['posted_by'];
          $date_added = $comment['date_added'];
          $removed = $comment['removed'];
  
          //Timeframe
  
          $date_time_now = date("Y-m-d H:i:s");
          $start_date = new DateTime($date_added); // Time of Post
          $end_date = new DateTime($date_time_now); // Current time
          $interval = $start_date-&gt;diff($end_date); // Difference between 
          dates
          if($interval-&gt;y &gt;= 1) {
              if($interval == 1)
                  $time_message = $interval-&gt;y . " year ago"; // 1 year ago
              else
                  $time_message = $interval-&gt;y . " years ago"; // 1+ year ago
          }
          else if ($interval-&gt; m &gt;= 1) {
  
              if($interval-&gt;d == 0) {
                  $days = " ago";
              }
              else if($interval-&gt;d == 1) {
                  $days = $interval-&gt;d . " days ago";
              }
              else {
                  $days = $interval-&gt;d . " days ago";
              }
  
               if($interval-&gt;m == 1) {
                  $time_message = $interval-&gt;m . " month". $days;
               }
               else {
  
                  $time_message = $interval-&gt;m . " months". $days;
               }
          }
          else if($interval-&gt;d  &gt;=1) {
              if($interval-&gt;d == 1) {
                  $time_message = "Yesterday";
              }
              else {
                  $time_message = $interval-&gt;d . " days ago";
              }
          }
          else if($interval-&gt;h &gt;= 1) {
              if($interval-&gt;h == 1) {
                  $time_message = $interval-&gt;h . " hour ago";
              }
              else {
                  $time_message = $interval-&gt;h . " hours ago";
              }
          }
          else if($interval-&gt;i &gt;= 1) {
              if($interval-&gt;i == 1) {
                  $time_message = $interval-&gt;i . " minute ago";
              }
              else {
                  $time_message = $interval-&gt;i . " minutes ago";
              }
          }
          else {
              if($interval-&gt;s &lt; 30) {
                  $time_message = "Just now";
              }
              else {
                  $time_message = $interval-&gt;s . " seconds ago";
              }
          }
  
          $user_obj = new User($con, $posted_by);
          ?&gt;
          &lt;div class="comment_section"&gt;
          &lt;a href="&lt;?php echo $posted_by?&gt;" target="_parent"&gt;&lt;img src="&lt;?php 
           echo $user_obj-&gt;getProfilePic(); ?&gt;" title="&lt;?php echo $posted_by; 
           ?&gt;" style="float:left;" height="30"&gt;&lt;/a&gt;
          &lt;a href="&lt;?php echo $posted_by?&gt;" target="_parent"&gt; &lt;b&gt;&lt;?php echo 
          $user_obj-&gt;getFirstAndLastName(); ?&gt; &lt;/b&gt; &lt;/a&gt;
          &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;?php echo $time_message . "&lt;br&gt;" . 
          $comment_body; ?&gt;
          &lt;hr&gt;
          &lt;/div&gt;
          &lt;?php
  
       } 
       }
  
      else {
  
      echo "&lt;center&gt;&lt;br&gt;&lt;br&gt;No comments to show&lt;/center&gt;";
       }
  
      ?&gt;
  
  
  
    &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>