I have built a WYSIWYG editor tool which allows me to create documents and push them to the front end of my website which works great, however, when I am logged in I require it to take the user_id from my logged in user and associate it with the doc they create.
I am using sessions:
<?
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=?");
}
?>
I have a form which allows me to create the doc and updated the databse as it should do but the one last column I need is for the form to post in the ID of the person who is currently logged in. Here is the form and the script that inserts it.
<?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>
And here is where the action takes place, as you can see it includes the session on the SQL insert but it just places a 0 value in the user_id column and does not take the users_id from the $_SESSION
<?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();
}
}
?>