-2

I have a login page which creates $_SESSION['username']= $username and $_SESSION['id'] = $id

now I have a shopping cart and I want to get that id so I can get the rest of the user's information

i tried $_SESSION['order'] = $_SESSION[id']; but it doesn't work. I'm still learning PHP and I don't know how to get these data from that session.

<?php 

include 'dbConfig.php';


include 'Cart.php';
$cart = new Cart;


if($cart->total_items() <= 0){
    header("Location: ");
}


$_SESSION['order'] = $_SESSION[id'];


$query = $db->query("SELECT * FROM members WHERE id = ".$_SESSION['id']);
$custRow = $query->fetch_assoc();
?>
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Cli Ad
  • 83
  • 7
  • 3
    check this line $_SESSION['order'] = $_SESSION['id']; – Kmg Kumar Dec 26 '18 at 06:52
  • First try to troubleshoot as follows echo '
    '; var_dump($_SESSION); echo '
    '; See whether the id is set or not once. And if the id is set in session then change $_SESSION in the query to static id, say 8 and check whether the query works. Hoping you can do this throw some checkings Note: See whether the session start is required
    – Hariharan V. Dec 26 '18 at 06:55
  • 1
    Your code contains a parse error as posted. It's also open to SQL injection; use a prepared statement. – Funk Forty Niner Dec 26 '18 at 15:17

2 Answers2

-1

You need to use session_start() at the top of the page

A single quotes is missing on $_SESSION['order'] = $_SESSION[id']; replace it with $_SESSION['order'] = $_SESSION['id'];

<?php 
session_start();
include 'dbConfig.php';


include 'Cart.php';
$cart = new Cart;


if($cart->total_items() <= 0){
    header("Location: ");
}


$_SESSION['order'] = $_SESSION['id'];


$query = $db->query("SELECT * FROM members WHERE id = ".$_SESSION['id']);
$custRow = $query->fetch_assoc();
?>

NOTE: You must confirm the session value is already set, please echo the session value and confirm it's not null

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
JIJOMON K.A
  • 1,250
  • 2
  • 11
  • 29
-1

I believe so you forgot to write this on top of the other page.

session_start()

Please do write it, and than check it by

print_r($_SESSION)