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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题