title: PHP putting last inserted row into a session
body: <p>I'm working on a school project, and I'm currently working on a form.
  I'm trying to insert the last inserted row from a table in my database into a session, so I can reuse the info when they want to go back to the form to adapt something.</p>
  
  <p>So the data is being inserted into the database, but when I try to put the variable $insertedGegvens into a session, it returns null, and I don't understand why.
  I currently have this:</p>
  
  <p>PDO:</p>
  
  <pre><code>public function selectById($id) {
      $sql = "SELECT * FROM `gegevens` WHERE `id` = :id";
      $stmt = $this-&gt;pdo-&gt;prepare($sql);
        $stmt-&gt;bindValue(':id', $id);
        $stmt-&gt;execute();
    }
  
    public function insert($data) {
      $errors = $this-&gt;validate( $data );
      if (empty($errors)) {
        $sql = "INSERT INTO `gegevens` 
                          (`naam`, `gemeente`, `postcode`, `adres`, 
                          `huisnr`, `email`, `tel`) 
                  VALUES (:naam, :gemeente, :postcode, :adres, 
                          :huisnr, :email, :tel)";
        $stmt = $this-&gt;pdo-&gt;prepare($sql);
        $stmt-&gt;bindValue(':naam', $data['naam']);
        $stmt-&gt;bindValue(':gemeente', $data['gemeente']);
        $stmt-&gt;bindValue(':postcode', $data['postcode']);
        $stmt-&gt;bindValue(':adres', $data['adres']);
        $stmt-&gt;bindValue(':huisnr', $data['huisnr']);
        $stmt-&gt;bindValue(':email', $data['email']);
        $stmt-&gt;bindValue(':tel', $data['tel']);
        if ($stmt-&gt;execute()) {
          return $this-&gt;selectById($this-&gt;pdo-&gt;lastInsertId()); //insert returns last row inserted through function selectById
        }
      }
      return false;
    }
  
    public function validate( $data ){
      $errors = [];
      if (!isset($data['naam'])) {
        $errors['naam'] = 'Gelieve een naam in te vullen';
      }
      if (!isset($data['gemeente'])) {
        $errors['gemeente'] = 'Gelieve een gemeente in te vullen';
      }
      if (!isset($data['postcode'])) {
        $errors['postcode'] = 'Gelieve een postcode in te vullen';
      }
      if (empty($data['adres']) ){
        $errors['adres'] = 'Gelieve een adres in te vullen';
      }
      if (empty($data['huisnr']) ){
        $errors['huisnr'] = 'Gelieve een huisnummer in te vullen';
      }
      if (empty($data['email']) ){
        $errors['email'] = 'Gelieve een email in te vullen';
      }
      if (empty($data['tel']) ){
        $errors['tel'] = 'Gelieve een telefoonnummer in te vullen';
      }
      return $errors;
    }
  </code></pre>
  
  <pre><code>  if($_POST['action'] == 'toCheckout'){
      $insertedGegevens = $this-&gt;orderDAO-&gt;insert($_POST);
  
      //trying to insert the info it just inserted into a SESSION['user'] so I can reuse this info when user returns to form
      $_SESSION['user'] = array();
      $_SESSION['user'] = $insertedGegevens;
      //$_SESSION['user'] returns null
  
      if (!$insertedGegevens) {
        $error = $this-&gt;orderDAO-&gt;validate($_POST);
        $this-&gt;set('errors', $error);
      }
      $_SESSION['user'] = array();
      $_SESSION['user'] = $insertedGegevens;
      header('Location: index.php?page=checkout');
      exit();
  }
  </code></pre>
  
  <pre><code>
  </code></pre>