Page 1 of 1

Getting the Id of a user and storing it in another table

Posted: Wed Sep 07, 2016 10:46 am
by Imy
Hi, I'm new to PHP Developers Network forum. I have a bit of PHP problem. I am also new to PHP, though I understand it to an extent.

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');
}
?>
Thanks for your help. Hope my question is well understood. :)

Re: Getting the Id of a user and storing it in another table

Posted: Wed Sep 07, 2016 12:00 pm
by Celauran
You already have their username stored in session data. You could use that to retrieve their ID.

Re: Getting the Id of a user and storing it in another table

Posted: Wed Sep 07, 2016 12:31 pm
by Christopher
Yes, typically you would get the ID of the logged-in user from the session. When a user logs in, set in the session values like the User ID, login status and any other information you might need (but not fields like password). On pages that require being logged-in to access certain features, check the session to see if the user has access. You would also get the ID of the user to save in records like your articles table.

On a separate note, I would recommend more consistency in the naming in your database. You have:[text]article_tbl contain columns art_id, memb_id,article_title, article_content. While the members_tbl contains memb_id, Firstname, Lastname, Username, Password, Email.[/text]
- Table names do not need "_tbl" on the end.
- Be consistent. You have article_tbl and members_tbl. Those should be articles and members (either both plural or singular -- pick one)
- Be consistent. You have field names that are abbreviated, all lower case, initial cap. I would recommend all lowercase. And the convention is for the table key to be named "id" while foreign keys are named "<table>_id". So use field names referring to other table keys like "members_id" or "articles_id".

Re: Getting the Id of a user and storing it in another table

Posted: Thu Sep 08, 2016 3:52 am
by Imy
Christopher wrote:Yes, typically you would get the ID of the logged-in user from the session. When a user logs in, set in the session values like the User ID, login status and any other information you might need (but not fields like password). On pages that require being logged-in to access certain features, check the session to see if the user has access. You would also get the ID of the user to save in records like your articles table.

On a separate note, I would recommend more consistency in the naming in your database. You have:[text]article_tbl contain columns art_id, memb_id,article_title, article_content. While the members_tbl contains memb_id, Firstname, Lastname, Username, Password, Email.[/text]
- Table names do not need "_tbl" on the end.
- Be consistent. You have article_tbl and members_tbl. Those should be articles and members (either both plural or singular -- pick one)
- Be consistent. You have field names that are abbreviated, all lower case, initial cap. I would recommend all lowercase. And the convention is for the table key to be named "id" while foreign keys are named "<table>_id". So use field names referring to other table keys like "members_id" or "articles_id".

Thanks. Correction Duly noted.

Re: Getting the Id of a user and storing it in another table

Posted: Thu Sep 08, 2016 3:53 am
by Imy
Celauran wrote:You already have their username stored in session data. You could use that to retrieve their ID.
\

I will try that. Thanks