I am building a website that enables users submit articles that they write. The site allows users to register, log in, and submit articles. I have two tables for now, MEMBERS_TBL, ARTICLE_TBL.
The article_tbl contain columns art_id, memb_id,article_title, article_content. While the members_tbl contains memb_id, Firstname, Lastname, Username, Password, Email.
I have successfully written the register and log in script, but an aspect in the article script is giving me problem.
When a users registers, an 'id' is automatically assigned to the user in the database. So when the user logs in, the user is taken to a page where he can write an article.
My problem is how can I get the 'id' of the user, that is, when the user submits the article, the article is stored in the article_tbl in db along side the id given to the user which is stored in the column 'memb_id' in the article_tbl.
I have an idea, but I don't know how to implement it. I know you can make use of sessions to get the 'id' of the user, but after that I'm clueless as to what to do next.
Here are some of the codes:
Code: Select all
//*************** Write Article Script **************************
<?php
include("check.php");
if (isset($_POST['submit'])){
if (empty($_POST['title']) || empty($_POST['content']) || empty($_POST['tag'])){
$error = "One or more of the fields are missing";
}
else{
$title = $_POST['title'];
$content = $_POST['content'];
$title = mysqli_real_escape_string($con, $title);
$content = mysqli_real_escape_string($con, $content);
$sql = "INSERT INTO article_tbl (article_title, article_content) VALUES ( '$title', '$content')";
$result = mysqli_query($con, $sql);
if ($result){
echo "Article Successfully Uploaded";
}
else{
$error = "Article not uploaded";
}
}
}
<div class="maark-edit graph">
<form action="" method="POST" enctype="multipart/form-data">
<?php echo $error; ?>
<div class="form-group">
<input class="form-control" name="artno" type="text" placeholder="Article Number">
</div>
<div class="form-group">
<input class="form-control" name="title" type="text" placeholder="Article Title">
</div>
<div class="md-editor" id="1454925081632">
<textarea name="content" data-provide="markdown" rows="15" class="md-input" placeholder="Article Content" style="resize: none;"></textarea>
<div class="md-fullscreen-controls">
<a href="#" class="exit-fullscreen" title="Exit fullscreen"><span class="glyphicon glyphicon-fullscreen"></span></a>
</div>
</div>
<hr>
<button type="submit" class="btn" name="submit">Submit</button>
</form>
</div>
Code: Select all
//****************************** Check Session (check.php) ***********
<?php
session_start();
include ('includes/connection.php');
$user_check = $_SESSION['username'];
$result = mysqli_query($con, "SELECT Username FROM members_tbl WHERE Username = '$user_check'");
$row = mysqli_fetch_array ($result, MYSQLI_ASSOC);
$login_user=$row['Username'];
if (!isset($user_check)){
header('Location:login.php');
}
?>