未定义的索引错误

It occurs undefined index error for the first time while redirecting to the same page after login, how can I solve this problem?

Here's my code:

code on index-page

<?php
session_start();
$error = $_SESSION['error'];
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db_food", $conn);
$row = mysql_query("select * from tbl_temp order by id DESC", $conn);
$row = mysql_fetch_array($row);
$user = $row['user'];
$pass = $row['pass'];
?>

code for the page After form submission

<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if($username =='' || $password == '') {
    $error = "Username or Password cant' be empty......";
    header("location: index.php");
} else {
    $data = mysql_query("select * from tbl_user where username='$username' && password='$password'", $conn);
    $num = mysql_num_rows($data);
    if($num==1) {
        $row = mysql_fetch_array($data);
        $_SESSION['name'] = $row['name'];
        $_SESSION['id'] = $row['id'];
        $_SESSION['user'] = $row['username'];
        exit;
    } else {
        $error= "Either Username or Password wrong!!!";
        header("location: index.php");
    }
}
$_SESSION['error'] = $error;
?>

I want to display the error message in the index page.

check first by isset

$error = "";
if(isset($_SESSION['error'])){
  $error = $_SESSION['error'];
}