First, you are not validating $_POST['apt'] before using it in your mysql query.
$apt = $_POST['apt'];
$query="SELECT * FROM payments Where apt='$apt'";
Always validate data, even if it is coming from an admin panel.
Second, separate your output and your business logic to make things easier to maintain.
i.e.
if(!empty($_POST)){
// process POSTed data
if(empty$_POST['name']))
$error = 'Name cannot be empty.';
// set success or error message to display
if(!isset($error)){
// run mysql query
// if success
$success = 'Success';
}
}
if(empty($_POST) || isset($error)){
if(!empty($error))
echo $error;
// show form
} else {
// show the message from the above outcome
echo $success;
}
Obviously this is a very trimmed down example and is just my opinion.
After you separate things out a bit, you should see a good spot do to what you are asking. If not, re-post.