title: php store session variable in mysql database
body: <p>I have a website in PHP. I try to store the session variable <code>$_SESSION['user_name']</code> to a mysql database when a logged in user visits a specific webpage on my site.</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "user1";
  $password = "user1";
  $dbname = "payment";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
  die("Connection failed: " . $conn-&gt;connect_error);
  }
  $sql = 'INSERT INTO users 
  VALUES ('.$_SESSION['user_name'].')';
  
  if ($conn-&gt;query($sql) === TRUE) {
  echo "New record created successfully";
  } else {
  echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  
  $conn-&gt;close();
  ?&gt; 
  </code></pre>
  
  <p>Error message:  </p>
  
  <blockquote>
    <p>Notice: Undefined variable: _SESSION in /opt/lampp/htdocs/succes.php on line 16</p>
  </blockquote>
  
  <p>Tried a bunch of things but can't figure it out. What is wrong here?</p>