0

I'm new to the Sessions topic so, I've been trying to get the logged person's user_id(column) from the user_login table using sessions and insert it into the another table but couldn't achieve.I would appreciate if anyone could help me out on how to achieve.

Thank you in advance.

Here's my code where I want to insert that user_id

<?php 
ob_start();
include ('header.php');
require('includes/connect.php');
require('includes/product.php');

$product = new Product;         
if(isset ($_GET['id'])) {
    $id = $_GET['id'];
    $data = $product -> fetch_data($id);    

    if(isset($_POST['add'])){
        if (isset($_SESSION['logged_in'])) {
            //$user_id = $_SESSION['user_id'];
            $query = $pdo -> prepare("INSERT INTO cart_items(product_id ,  Price) VALUES (?,?)");
            $query -> bindValue(1, $id);
            //$query -> bindValue(2, $user_id); //This is where I want to insert logged user's user_id in to the database.
            $query -> bindValue(2, $data['new_price']);
            $query ->execute();
            header('location:cart.php');
        }
        else{
            header('location:Login Page.php');
        }
    }
?>

Here's my Login.php(Login Page)

<?php
include ('header.php');
include ('includes/connect.php');
if( ! isset($_SESSION['logged_in'])) {
    if(isset($_POST['email'] , $_POST['password']))
    {
            $username = $_POST['email'];
            $password = md5($_POST['password']);
            if(empty($username) or empty($password))
            {
                $error = "Please fill all the fields";
            }
            else
            {
                $query = $pdo->prepare("SELECT * FROM user_login WHERE name = ? AND password = ? ");
                $query->bindValue ( 1, $username);
                $query->bindValue ( 2, $password);
                $query->execute();
                $num=$query->rowCount();

                if($num==1) {
                    $_SESSION['logged_in']= true;
                    header('location :' .$_SESSION['redirectURL']);
                    //header('location: index.php');
                    exit();
                }
                else{
                    $error = "Please enter correct Username and Password";
                }
            }
    }
}
else{
    $error = "you have already logged in";
}
?>
 <div class="login">
            <div class="container">
                <div class="login-grids">
                    <div class="col-md-6 log">
                             <h3>Login</h3>
                             <div class="strip"></div>
                             <p>Welcome, please enter the following to continue.</p>
                             <form action="" method="post">
                                <?php if(isset($error)){ ?>
                                        <small style = "color : #aa0000"; ><?php echo $error ?></small>
                                        <br><br>
                                <?php } ?>
                                <div class="form-group">
                                    <label for="email"  class="head">Email</label>
                                    <input class="form-control login-box" name="email"  type="text" id="email"/>
                                </div>
                                <div class="form-group">
                                    <label for="password"  class="head">Password</label>
                                    <input class="form-control login-box" name="password"  type="password" id="password"/>
                                </div>          
                                 <button type="submit" class="btn login-btn">Submit</button>
                             </form>
                            <a href="#">Forgot Password ?</a>
                    </div>
                    <div class="col-md-6 login-right">
                            <h3>New Registration</h3>
                            <div class="strip"></div>
                            <p>By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.</p>
                            <a href="register.html" class="button">Create An Account</a>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
5
  • Try adding: session_start(); before you call the $_SESSION variable When I need to check if a user is logged in I create a separate security.php like: session_start(); if($_SESSION['logged_in']=="yes") { } else { session_start(); session_destroy(); header('Location: index.php'); exit(); } Jun 29, 2017 at 19:06
  • sorry I read wrong, to achieve this you need to save the user id on another session variable and then you can use it on the insert on the other php data. On the session variables you can define as much variables as needed. Jun 29, 2017 at 19:29
  • Do you have any idea on how to save user_id in a variable? Jun 29, 2017 at 19:31
  • sure, let me post an answer to show you how only that I have it on mysql you only need to adapt it. Jun 29, 2017 at 19:33
  • Issues specific to programming and software development are off topic, see On-Topic. Try Stack Overflow but please first read How do I ask a good question?.
    – DavidPostill
    Jun 29, 2017 at 20:22

1 Answer 1

1

You can get the user_id with:

$query=mysql_query("SELECT * FROM user_login WHERE name = '$username' AND password = '$password');

$row=mysql_fetch_assoc($query);
$num=mysql_num_rows();

if($num==1)
{
    session_start(); 
    $_SESSION['user_id']= $row['user_id'];
}

I'm not sure but I think that the mssql_fetch_assoc if the php command to get the fetch assoc for sql.

The 'user_id' inside the $row brackets is the name of the column on your table

Not the answer you're looking for? Browse other questions tagged .