# PHP mail form and mySQL

**URL:** <https://forum.kirupa.com/t/php-mail-form-and-mysql/210390>\
**Category:** programming\
**Created:** [December 18, 2006, 2:01pm UTC](https://forum.kirupa.com/t/php-mail-form-and-mysql/210390 "2006-12-18T14:01:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![g\_rochford](https://avatars.discourse-cdn.com/v4/letter/g/c57346/32.png) [@g\_rochford](https://forum.kirupa.com/u/g_rochford)\
**Post date:** [December 18, 2006, 2:01pm UTC](https://forum.kirupa.com/t/php-mail-form-and-mysql/210390/1 "2006-12-18T14:01:49Z")

</div>

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
<?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')");
}
 
?>

```
