Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

James Barrett
James Barrett
13,253 Points

Why is my error message not echoing to the screen?

When the user submits a form, the PHP will check to see if the $post was empty, if so, it will set an $error_message variable; thus database INSERT query will not execute. I then attempt to show the error message further on in the code, however it will not display. I have been trying to figure this out for hours and I still cannot find the solution. The funny thing is, this used to work, however I must have implemented something in this file which is preventing the code to execute correctly.

PHP:

    <?php require("inc/db.php"); ?>
    <?php include("inc/functions.php"); ?>
    <?php include ("inc/ChromePhp.php"); ?>
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    ini_set("html_errors", 1);
    session_start();
    if(!isset($_GET['id'])) {
      header("Location: ?id=".getUserId($_SESSION['U_Email']));
    }
    if(isset($_SESSION["U_Email"])) {
      $usersData = getUserInfo($_GET['id']);
      $postCount = getPostCount($_GET['id']);
    } else {
      header('Location: login.php'); 
    }

    if($_SERVER["REQUEST_METHOD"] == "POST") {
      $post = trim(filter_input(INPUT_POST,"user_post",FILTER_SANITIZE_SPECIAL_CHARS));

      if($post == "") {
        $error_message = "Please enter something before submitting";
      }

      if(!isset($error_message)) {
        $post = $db->real_escape_string($post);
        $postStore = $db->query("INSERT INTO `Post`(P_Body, P_Dateadded, User_U_ID)     VALUES ('{$post}', NOW(), '{$_SESSION['U_ID']}' )");
      }
    }

    $dashNav = true;
    $cap = true;
    $pageTitle = 'Profile';
    $name = $usersData['U_Forename'] . " " . $usersData['U_Surname'];
    $gender = $usersData['U_Gender'];
    $bio = $usersData['U_Biography'];
    $team = $usersData['U_Team'];
    $city = $usersData['U_City'];
    include("inc/header.php");
    ?>

      <?php if(userExists($_GET['id'])) { ?>
      <section>
        <div class="wrapper">
          <?php
          if($_SESSION['U_ID'] != $_GET['id']) {
            $testFollow = "SELECT `Following`.F_ID, `Following`.U_ID FROM `Following`
            WHERE U_ID = '{$_SESSION['U_ID']}' AND F_ID = '{$_GET['id']}'";
            $testFollowResult = $db->query($testFollow);
            if ($testFollowResult->num_rows > 0) {
              echo "<button class='lift' href='#'>Unfollow Driver</button>";
            } else {
              echo "<button class='lift' href='#'>Follow Driver</button>";
            }
          }
          ?>

          <?php if (isset($error_message)) {
            // This code will not echo!
            echo "<h2>".$error_message."</h2>";
          }
          ?>

    <div class="section-b">
     <div class="grid">
      <div class="row">
       <div class="col-wd-12">
        <div class="col">
         <form id="share" name="share" action="profile.php" method="post">
         <textarea id="post" name="user_post" placeholder="What's happening?">    </textarea>
         <span id="errorpost" class="error">You must input something something before sending</span>
         <?php
         echo "<div class='g-recaptcha' data-     sitekey='6LfNTB0TAAAAAKEw9zfnFzvGCXF9MuYTkdB144x1
         '></div>"; ?>
       <button onclick="return postCheckValidate();"        type="submit">Share</button>
     </form>
         </div>
        </div>
       </div>
      </div>
     </div>
<?php } ?>



Any suggestions would be fantastic.

Thanks,
James.
David Bath
David Bath
25,940 Points

I can't really explain why it isn't working, but I would check the values of certain variables immediately after they should be getting set. For instance:

if($post == "") {
  $error_message = "Please enter something before submitting";
  echo $error_message;
  exit;
}

I would do something similar inside the block where you check if $error_message is set.

1 Answer

Joe Schultz
Joe Schultz
6,191 Points

Try this: php<?php if (!isset($error_message)) { // This code will not echo! echo "<h2>".$error_message."</h2>"; }

Joe Schultz
Joe Schultz
6,191 Points

php<?php if (!isset($error_message)) { // This code will not echo! echo "<h2>".$error_message."</h2>"; }