Undefined Index: User_Id. Where, User_Id Is Session Variable

Undefined index: user_id. Where, user_id is session variable

I understand the problem. I wasn't starting my session in the page where I'm inserting data into users table.

Now I put session_start();

And it runs perfectly fine.

Here code:

session_start();
// Add register user
if(isset($_POST["register_btn"]))
{
extract(array_map("test_input", $_POST));
$md5pass=md5($password);

$m->set_data('full_name',$full_name);
$m->set_data('gender',$gender);
$m->set_data('email',$email);
$m->set_data('mobile',$mobile);
$m->set_data('password',$md5pass);
$m->set_data('created_date',$date);
$m->set_data('updated_date',$date);

$a1= array ('role_id'=> 2,
'full_name'=> $m->get_data('full_name'),
'gender'=> $m->get_data('gender'),
'email'=> $m->get_data('email'),
'mobile'=> $m->get_data('mobile'),
'password'=> $m->get_data('password'),
'created_date' => $m->get_data('created_date'),
'updated_date' => $m->get_data('updated_date'));

$last_auto_id=$d->last_auto_id("users");
$res=mysqli_fetch_array($last_auto_id);
$user_id=$res['Auto_increment'];

// inserting into users table
$insert=$d->insert('users',$a1);

if($insert>0)
{
// I am starting session here
$_SESSION['user_id']=$user_id;
header("location:account.php?msg=Your account has been created.");
}
else {
header("location:account.php?msg=Error.");
}
}

Session id is not showing with an error message Notice: Undefined index: UserID

In your login page you're assigning the variable as this:

$_SESSION["User ID"] = $row['UserID'];

But on the home page you're looking for UserID.

Change:

$_SESSION["User ID"] = $row['UserID'];

to

$_SESSION["UserID"] = $row['UserID'];

The point you're getting your error from is from your usage in the HTML where you say:

<h4>Welcome:<?php echo $_SESSION{"UserID"};?>to your page</h4>

In the first instance you're using isset(), but here you're suddenly assuming it's there and ready for use. Either way you should edit the User ID variable, so this stops throwing an error. You might also want to add an isset() statement here.

P.S. Also heed Victor Perez's answer concering your password. And don't store them as plain text. You're literally just checking the POST password to the password in your database, that's no good. Also I might advice you to move away from using such query's and start using prepared statements.

Undefined index: userID error

Add this at the top of each file:

if(!isset($_SESSION)) session_start();

Also, when you do:

$userID = $_POST['userID'];

you should ensure that $_POST['userID'] exists:

if(isset($_POST['userID'])) $userID = $_POST['userID'];

PHP session amd notice: undefined index ...

It should be $_SESSION not $_session.

 $_SESSION['user_id']=$row['user_id'];

$user_id = $_SESSION['user_id'];

$_SESSION is a global variable and should be written in capital. When you are using the variable with small letters, in that case it doesn't get the variable because its never been defined and doesn't have the key user_id and hence you get the error. Hope this helps.

PHP: Notice: Undefined index where the session variable is defined

This came down to the basics of debugging/troubleshooting.

  1. Understand as much as you can about the technique/library/function/whatever that you're trying to use.
  2. Inspect the salient bits and make sure that they are what you expect or what they should be. (There's a slight difference between those two, depending on the situation.)
  3. If that doesn't bring you towards a solution, step back and make sure you're understanding the situation. This may mean simplifying things so that you're only dealing with the issue at hand, i.e. create a separate, simpler test case which exposes the same problem. Or, it may simply mean that you stop coding and work through the flow of your code to make sure it is really doing what you think it is doing.

A typical issue with sessions not working is forgetting to use session_start() (near or at the top) of any page which uses sessions.

One of my favorite snippets of PHP code, for debugging:

print '<pre>';
var_dump($some_variable);
print '</pre>';

I try to use print for debugging and echo for regular output. It makes it easier to spot debugging code, once it's goes beyond a few trivial bits of output.

Meanwhile, var_dump will print a bit more info about the variable, like it's type and size. It's important to wrap it in <pre></pre> so that it's easier to read the output.

Codeigniter 4 Undefined index: user_id

As by your comment (image) your array looks like:

Array
(
[0]=>Array
(
[user_id]=>1,
[user_name]=>'test',
//etc.
)
)

You get the

Undefined index: user_id

error message, because of addressing wrongly the array while using 'user_id' => $user['user_id']

the correct way is to add the index you want to retrieve like:

$this->setUserSession($user[0]);   // where 0 can be changed to the index you pretend

now the array is flattened and 'user_id' => $user['user_id'] doesn't throw an error anymore.



Related Topics



Leave a reply



Submit