N
N
NekoAsame2020-05-25 11:09:15
PHP
NekoAsame, 2020-05-25 11:09:15

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>


I found this part in a similar question, but it still does n't work . information is not displayed, I don't understand why..

$users_id = $_SESSION['user']->id;

5ecb82e12f311433137889.jpeg
$users_id = $_SESSION['user']->id;

5ecb832f93bf2278974463.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-25
@FanatPHP

Just like any other variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question