Search code examples
phpmysqlmysqliphp-5.3

PHP/MySQL text to DB table - query hits but text empty


Not quite sure where I am going wrong. Do I need to encode the text? When I echo as a test it is working fine. The DB is being reached as well. But when the query hits, there is no text.

<?php
include($_SERVER['DOCUMENT_ROOT'].'/includes/dbh.php');
$newNote = mysqli_real_escape_string($conn, $_POST['note']);

if (empty($newNote)){
        header("Location: /admin/notes.php?error=empty");
        exit();
} else {
        $sql = "INSERT INTO notes (note) VALUES ('$NewNote')";
        $result = mysqli_query($conn, $sql);
        header("Location: /admin/notes.php?success");
    }
?>


<div class="section">
  <div class="container">
    <?php
     $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     if (strpos($url, 'error=empty') !== false) {
       echo "<div class='alert alert-danger'>Error: You must enter some text!</div>";
     }
     elseif (strpos($url, 'success') !== false) {
       echo "<div class='alert alert-success'>Note successfully submitted!</div>";
     }
    ?>
    <h3>Add Note</h3>
    <form action="/admin/includes/notes.inc.php" method="post">
      <div class="form-group">
        <textarea name="note" class="form-control m20" rows="5"></textarea>
        <button type="submit" name="submit" class="btn btn-default"><span class="glyphicon glyphicon-plus"></span> Submit</button>
      </div>
    </form>
  </div>
</div>

Solution

  • Variables in PHP are case sensitive.
    You just need to lowercase the first "N" of the $NewNote variable in the query insert line and it should work.