Is there a way to post a session variable?
-
26-12-2019 - |
Domanda
I am looking to access a logged in user_id that is stored in the session, and post it in a query. So something like:
if(!empty($_SESSION['user_id'])) {
$_POST['user_id'] = $_SESSION['user_id'];
$ui = $_POST['user_id'];
} else {
$errors[] = 'Please login before posting a book for sale.';
}
and then in the query:
if (empty($errors)) {
$q = 'INSERT INTO books (user_id, isbn, name, image, price) VALUES (?, ?, ?, ?, ?)';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'isssd', $ui, $isbn, $bn, $i, $p);
mysqli_stmt_execute($stmt);
Now I already know that the first IF statement isn't correct, but I posted it like that so you PHP'ers can see what I am trying to accomplish. I am trying to figure how to take the stored variable user_id, make it sticky, and then post it in the query. Thanks!
Soluzione
Since you already have your variable stored in $_SESSION['user_id']
..
Why don't you just use this:
$ui = $_SESSION['user_id'];
Altri suggerimenti
Just put session id
in a variable. no need to write any POST
variable.
You can use like this :
$your_variable = $_SESSION['user_id'];
//according to your code you can write it
$ui = $_SESSION['user_id'];
And surely don't forget to use session_start();
at top of your page.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow