
Recap: Part 1 | Part 2 | Part 3
First we need to build a PHP file that we can edit. Create a new file on your webserver and name it whatever you want (I called mine "pushtodb.php"). Now open that file with your editor of choice. I like notepad++ but you can use whatever is more comfortable for you.
Just like in HTML, we have to open the PHP script with <?PHP (and don't forget to close the tag now so you don't forget later - close it with ?> ). This tells the browser that you are going to use PHP as oppose to just HTML.
Lets create a variable to open up a connection to the SQL server. You do that with this command:
Now we have to tell the SQL server which database to use. This command will connect to the SQL server and open up the proper database:
Notice the "$con" in that statement? That will take the first statement that we wrote and inject it into this one. Trust me, putting things in variables will make your life a LOT easier as you can reuse the variables throughout the script.
Time to write that SQL statement that we will actually insert values into the database. Let's make this in a variable named "$sql" so we can use it over and over. At this point (if you read Part 1) you should have all your inputs from your webform in a text file so we can input them in the SQL statement. You have them in notepad++ right??? Right??? Here is the SQL statement; copy and paste this into notepad++ so you can edit it as you need to:
VALUES
('$_POST[input_1]','$_POST[input_2]','$_POST[input_3]')";
{
die('Error: ' . mysql_error());
}
$subject = 'New contract was submitted';
$body = "This is the body of the email:";
$text = $body. " " . $id;
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');