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>