How to get loged in user id and insert in article table mysql

Hi guys and gals,
I have a problem with inserting loged in user id in to article table,
I used $_REQUEST [id] and $_GET [id] both didnt work, spent 3 hours on it.
really need help
here is my session variables for loged in users. from login.php
session_start();
$_SESSION[‘id’] = $id;
$_SESSION[‘username’] = $username;
$_SESSION[‘password’] = $password;
$_SESSION[‘isAdmin’] = $isAdmin;

Here is my hedaer.php where session controls the pages
session_start();
require_once ‘…/connect.php’;
require_once ‘…/inc/functions.php’;

// If session variable is not set it will redirect to login page
if(!isset($_SESSION[‘username’]) || $_SESSION[‘isAdmin’]!=1){
$_SESSION[‘message’] = “You dont have permission to see this page!”;
header(“location: …/error.php”);
exit;
}

Just use the session-variable, no need for request or get.

both didnt work, spent 3 hours on it.

That means nothing for anybody who did not sit right next to you in front of your screen. Post comprehensible code. Check variables with var_dump().

I think I need to Store id in session but no clue how to do it.
Normally it should be stored when posted from login.php

I tried $_SESSION[id]

Didn’t work, it’s empty
I get username but no ID

$sql = “INSERT INTO articles (title, detail, user_id) VALUES (?, ?, ?)”;

    if($stmt = $conn->prepare($sql)){
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("ssi", $param_title, $param_detail, $param_user_id);
        
        // Set parameters
        $param_title = $title;
        $param_detail = $detail;
        $param_user_id = $id;

First thing is to have session_start() at the very start of all your PHP code. Do you have that, before any browser output?

You did put single-quotes around id as in your first post?

Yep it’s on the top of the header after <?php tag

Should I change my session in first post to

if(!isset($_SESSION["username"] = "$username";
$_SESSION["password"] = "$password";
$_SESSION["isAdmin"] = "1";
$_SESSION["id"] = "$id"){
$_SESSION['message'] = "You dont have permission to see this page!";
header("location: ../error.php");
exit;
}

No, that’ll just probably give parse errors. You seem to be mixing up checking whether the session variables exist, and assigning them values. I also don’t think you should store the password in a session variable, but that’s a different thing for later.

1 Like

Here is the soluton how to solve it.
for newbies, I had problem with login.php it was posting all infos exept user_id because of bind variables,
I needed to ad id to sql querry.
working perfect now.
querry:
$sql = "SELECT username, password, type, id FROM users WHERE username = ?";
bind variables :
$stmt->bind_result($username, $hashed_password, $type, $id);
Thanks
for answers and ideas @droopsnoot

Glad you’ve sorted it, that’s a bit of code you didn’t post before.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.