I do not understand, how should I fix it?
The problem is demonstrated by this:
<h1>Hello world!</h1>
<?php
header('location: http://www.example.com');
exit;
?>
The problem is that the HTML code causes output to be sent, and output cannot be sent before the headers are sent.
To fix the problem, do something like this:
<?php
if (isset($_POST['submit']))
{
// Connect to database server and select database
// ...
$query = "INSERT INTO table1 (id, month, year) VALUES ('$id', '$month', '$year')";
mysql_query($query) or die('Error, query failed. ' . mysql_error());
header('Location: index2.php');
exit();
}
?>
<html>......
<input type="submit" name="submit" value="Submit" />
By the way, note that the location URL should be an absolute URL. This is mentioned in the PHP manual's entry on [man]header/man.