Answer the question
In order to leave comments, you need to log in
How to write user id from $_SESSION to database table?
I need that when you click on the "Book" button, the user's id is determined and searched in the users table, and then this same id is written to the reservation table in the id_reader field. I define the id through the session like this, but I don’t understand how to find a record in the database and transfer it to another table ..
Or, immediately write the id obtained through the session to the database.
The code looks like this:
if (isset($_GET['res_id'])) { //проверяем, есть ли переменная
$res_id = mysqli_real_escape_string($link, $_GET['res_id']);
$result = mysqli_query($link, "SELECT id FROM `book` WHERE `id` = '$res_id'");
$row = mysqli_fetch_array($result);
$dir='';
$users_id = $_SESSION['user']->id;
// строку из таблицы
$sql = mysqli_query($link, "INSERT INTO `reservation`(`id_book`,`users_id`) VALUES ('$res_id', '$users_id')");
var_dump($sql);
if ($sql) {
echo "<h2>Данные о книге добавлены.</h2>";
} else {
echo '<p>Произошла ошибка: ' . mysqli_error($link) . '</p>';
}
}
?>
<form>
<a href="#"><?= $_SESSION['user']['id'] ?></a>
</form>
$users_id = $_SESSION['user']->id;
$users_id = $_SESSION['user']->id;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question