0

I'm expecting to see the data of a user after I login but it always show the warning, "Undefined array key "user_id" in C:\xampp\htdocs\farm-e-mart\home.php on line 5"

//home.php

    <?php
include 'config.php';

session_start();
$user_id = $_SESSION['user_id'];

if(isset($_GET['logout'])){
   unset($user_id);
   session_destroy();
   header('location:login_form.php');
}
?>

//login_form.php

    <?php

@include 'config.php';

session_start();

if(isset($_POST['submit'])){

   $name = mysqli_real_escape_string($conn, $_POST['name']);
   $email = mysqli_real_escape_string($conn, $_POST['email']);
   $pass = md5($_POST['password']);
   $cpass = md5($_POST['cpassword']);
   $user_type = $_POST['user_type'];

   $select = " SELECT * FROM user_form WHERE email = '$email' && password = '$pass' ";

   $result = mysqli_query($conn, $select);

   if(mysqli_num_rows($result) > 0){

      $row = mysqli_fetch_array($result);

      if($row['user_type'] == 'admin'){

         $_SESSION['admin_name'] = $row['name'];
         header('location:farmersite.php');

      }elseif($row['user_type'] == 'user'){

         $_SESSION['user_name'] = $row['name'];
         header('location:site.php');

      }
   }else{
      $error[] = 'incorrect email or password!';
   }

};

I don't know what to do and also I just started learning php, what can i do to fix this?

  • `$_SESSION['user_id']` is not set. You do `$_SESSION['user_name'] = $row['name'];`, should do that for `user_id` aswell. – geertjanknapen May 30 '22 at 11:00
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – CBroe May 30 '22 at 11:04
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 31 '22 at 07:04

0 Answers0