PHP mail form and mySQL

Hi all

I have set up a mySQL database on my server with a table called phil containing three lines email, subject and message Im using this code i found in a previous Kirupa post to try and input the data into the table but i cant seem to get it to work, it does send the email to my inbox but not to mySQL. can anybody help?

<?php

$sendTo = "email@company.com";
$subject = "Contact Form";
 
$headers = "From: " . $_POST["firstName"] ." ". $_POST["lastname"] . "<" . $_POST["email"] .">
";

$headers .= "Reply-To: " . $_POST["email"] . "
";

$headers .= "Return-path: " . $_POST["email"];
 
$message = $_POST["message"] . "
"; 
$message .= "email: " . $_POST['email'] . "
";
$message .= "subject: " . $_POST['subject'] . "
"; 

mail($sendTo, $subject, $message, $headers);
 
if($email=="undefined" || $subject=="undefined" || $message=="undefined"){
echo "Please fill in all the fields";
}else{
echo "Thankyou for registering.";
mysql_connect("localhost", "username", "password");
mysql_select_db("databasename");
mysql_query("INSERT INTO tablename (`email`,`subject`,`message`) VALUES ('$email','$subject','$message')");
}
 
?>