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
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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.