douyong1285 2017-06-26 10:25
浏览 31
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongliang1996 2017-06-26 11:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应