在查询中将会话用户名提交回数据库

在查询中将会话用户名提交回数据库

问题描述:

Basically i want whoever creates a note on my website to be the "author" of that note.

So whoever is logged in when they create the note should be the author.

At the moment in login_form.php i have created a session which is the "included" in my general_notes.php. In general_notes.php i have the following code for when the user clicks to add a note:

<p class="fa fa-plus hover-cursor icon-spin noSelect" data-toggle="modal" data-target="#addGeneralNote" style="font-size: 16pt;"></p>

which runs:

<!-- Modal -->
<div id="addGeneralNote" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header header-notes">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add General Note</h4>
      </div>
      <!-- Form submission data -->
      <form action="addNewNote.php" method="POST">
          <div class="modal-body">
            <p>Please enter the note you wish to create.</p>
            <textarea id="addGeneralNoteName" placeholder="Enter a note name..." name="title" maxlength="100"></textarea>
            <textarea id="addGeneralNoteText" placeholder="Enter note here..." name="content"></textarea>
            <input type="checkbox" name="pinned"> Pin Note to your homepage
          </div>
          <div class="modal-footer footer-notes">
            <button type="submit" class="btn btn-success">Create</button>
          </div>
      </form>
    </div>

  </div>
</div>

You'll see in the form there is addNewNote.php which runs:

<?php

if (isset($_POST['title'], $_POST['content'], $_SESSION['username'])) 
{
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_SESSION['username'];

    $stmt = "INSERT INTO Notes (NoteName, Note, Author, DateCreated) VALUES (?, ?, ?, GETDATE())";
    $params = array($title, $content, $author);

    $stmt = sqlsrv_query($conn, $stmt, $params);

    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }

    header('location: general_notes.php');
}

else
{
    echo "No Data";

}


?>

Before i added to the isset $_SESSION['username'] it ran fine.

At the moment it hits this part:

else
{
    echo "No Data";

}

of the isset function

So how how do i pass through the session username into my addNewNote.php script?

The simple answer is that you didn't call session_start() in addNewNote.php. But I'd also like to elaborate on a comment you made above, hopefully to help future readers:

i for some reason presumed it would get the session from the previosu page

The "previous page" was a separate HTTP request entirely, and the two have no connection to one another. Much in the same way that a JavaScript application re-starts with each page load, so does a PHP application start anew with each page load.

Consider each individual HTTP request to be its own separate instance of the application. While these instances can share data via external data stores, such as a database or session state, the application itself retains nothing in-memory about any other running or previous instance.

So while the data may indeed be in the session data store (which is external to the application itself), each instance of the application needs needs to connect to that data store in order to use it. Just as one must connect to a database in order to use it, one must also invoke session_start() in order to use the session.