Jump to content

Notice: Undefined index:


Recommended Posts

Please consider the following code:

 

<form method="post">
Passwrod: <input tyep="text" name="name" />
    <input type="submit"  value="Ok" />
</form>

$db_conn = new MySQLi('localhost', 'root', '', 'db');
$result = $db_conn->query("SELECT * FROM user_name");

$row = $result->fetch_assoc();

$fetched_name = $row['user_name'];

$input_name = $_POST['user_name']; // Notice: Undefined index error

if($fetched_name == $input_name)
{
   header("Location: welcome.php ");
}

*This code simply fetches user names from the db and if the user name exists then it redirect to welcome.php page.

 

 

When I run this code I get 'Notice: Undefined index:' error. Of course I can simply ignore this notice. But I don't want to have any error messages; I want to make my code CLEAN. Also I don't want to use the  @ operator to hide the error. I wonder If there is any better solution for this.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/203171-notice-undefined-index/
Share on other sites

You want some clean coding, you should watch for spelling mistakes. You spelled the word password incorrectly below.

 

Also, your PHP code is looking for "user_name" where you have not sent it, your form is sending data with "name" so take the below code and replace it with your HTML form and see how it works.

 

<form method="post">
Password: <input tyep="text" name="user_name" />
    <input type="submit"  value="Ok" />
</form>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.