将$ _SESSION id插入表中

时间:2015-05-11 20:15:00

标签: php mysql

我已经构建了一个WYSIWYG编辑器工具,它允许我创建文档并将它们推送到我的网站的前端,这非常有用,但是,当我登录时,我要求它从我登录的用户中获取user_id将它与他们创建的文档联系起来。

我正在使用会话:

<?
session_start();
session_regenerate_id();
if(!isset($_SESSION['user'])){
       header("Location: index.php");
    exit;
}
else{

 include ('../../db_con.php');

 $sql=$dbh->prepare("SELECT * FROM user_login WHERE id=?");

}
?>

我有一个表单,允许我创建文档并更新数据库,但我需要的最后一列是表单在当前登录的人的ID中发布。这是表单和插入它的脚本。

<?php include 'header.php'; ?>

<form action="actions/newDocAdd.php" method="post" id="rtf" name="">
    <input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />

    <button class="postEditBtn" type="button" onclick="ibold()" title="Bold Text"><i class="fa fa-bold"></i></button>
    <button class="postEditBtn" type="button" onclick="iitalic()" title="Italic Text"><i class="fa fa-italic"></i></button>
    <button class="postEditBtn" type="button" onclick="iunderline()" title="Underline Text"><i class="fa fa-underline"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontName()" title="Font Family"><i class="fa fa-font"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontsize()" title="Font Size"><i class="fa fa-text-height"></i></button>
    <button class="postEditBtn" type="button" onclick="ifontcolor()" title="Font Colour"><i class="fa fa-eraser"></i></button>
    <button class="postEditBtn" type="button" onclick="ihiliteColor()" title="Highlight Text"><i class="fa fa-magic"></i></button>
    <button class="postEditBtn" type="button" onclick="ilink()" title="Add/Edit Link"><i class="fa fa-link"></i></button>
    <button class="postEditBtn" type="button" onclick="iunlink()" title="Remove Link"><i class="fa fa-chain-broken"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyLeft()" title="Text align-left"><i class="fa fa-align-left"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyCenter()" title="Text align-center"><i class="fa fa-align-center"></i></button>
    <button class="postEditBtn" type="button" onclick="ijustifyRight()" title="Text align-right"><i class="fa fa-align-right"></i></button>
    <button class="postEditBtn" type="button" onClick="iUnorderedList()" title="Unordered List"><i class="fa fa-list-ul"></i></button>
    <button class="postEditBtn" type="button" onClick="iOrderedList()" title="Ordered List"><i class="fa fa-list-ol"></i></button>
    <button class="postEditBtnUndo" type="button" onClick="iUndo()" title="Undo last change"><i class="fa fa-rotate-left"></i></button>
    <button class="postEditBtnRedo" type="button" onClick="iRedo()" title="Redo last change"><i class="fa fa-rotate-right"></i></button>


    <br><br>

    <textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
    <iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>

    <br><br> 
    <input onclick="formsubmit()" type="submit" value="Create Document" name="submit"/>


</form>

这里是动作发生的地方,因为你可以看到它包含了SQL插入的会话,但它只是在user_id列中放置了一个值而没有从$ _SESSION中获取users_id

<?php

include_once '../../session.php';

if(isset($_POST["submit"])){
$hostname='#####';
$username='#####';
$password='#####';

try {

$dbh = new PDO("mysql:host=$hostname;dbname=#####",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

$sql = "INSERT INTO doc_list (doc_title, doc_content, doc_created, user_id) VALUES ('".$_POST["doc_title"]."','".$_POST["doc_content"]."', NOW(), '".$_SESSION['user']."' )";


if ($dbh->query($sql)) {
    header ('Location: ../docList.php');
}
else{
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>

0 个答案:

没有答案