MySQL
Php

How to insert data into mysql database using PHP form and then send mail – Part 2

Beginner 30 mintues
view more posts

insert-into-mysql-database-PHP-and-mailing-part-2

Welcome to the second part of the tutorial. Here we see how to insert the data into MySQL database and how to send an email using PHP. If you could keep up till this point then please give yourself a tap on your back. You have done a good job.

If not please go and read the FIRST PART of the tutorial for better understanding. The FIRST PART explains all the basics of processing a form in a detailed method.

In this tutorial we dive in deep to understand the do’s and dont’s while connecting with the MySQL database. We also learn about the naming conventions and how to increase the security of your code. Feel free to ask any questions in the comments section and we assure you that it will be cleared.

Insert the form values into the MySQL database using PHP

Now the next step in inserting the form values into MySQL database is to select the database. Now that the MySQL connection is made and assigned to $connection variable it is easy.

It’s time to introduce you to another MySQL function called mysql_select_db. This helps you select the database in which you want to insert the form values. mysql_select_db function has two attributes.

mysql_select_db("db_name", "connection_variable");

In our case it is website_db and $connection variable. So our code will be:

mysql_select_db("website_db", $connection);

Now that the database is selected let us insert the values. It is done with a simple MySQL query statement – INSERT. Below is the syntax

<?php
    $sql = "INSERT INTO table (coloumn1, coloumn2) VALUES ('value1', 'value2')";
?>

We replace the syntax with our values. In order to capture the error we do an if statement here with mysql_query function. Hence the following code:

<?php
    if (isset($_POST['enq_submit'])){
        $nfrm_name = $_POST['enq_name'];
        $nfrm_mobile = $_POST['enq_mobile'];
        $nfrm_email = $_POST['enq_email'];
        $nfrm_message = $_POST['enq_message'];
       
        $sql = "INSERT INTO tbl_enquiry (nfrm_name, nfrm_mobile, nfrm_email, nfrm_message)
                VALUES ('$nfrm_name', '$nfrm_mobile', '$nfrm_email', '$nfrm_message')"
;
        if (!mysql_query($sql,$connection)){
            die('Error: ' . mysql_error());
        }
    } else {
        echo("Kindly use the form to submit the data!");
    };
   
?>

That’s it. This is how you insert data into your MySQL database After testing the data when we check in phpMyAdmin we can see the table updated.

Enquiry table with data

How to send Mail from the PHP form?

The answer to this big question is actually very small. PHP helps us a great deal with this small function – mail(). The mail() function has its own attributes

mail($emailID, $subject, $body, $headers );

$emailID is the email id to which the mail has to be sent. To send mails to multiple id’s just separate them with a comma.

$emailID = "info@comany.com, ceo@comany.com";

$subject is the subject of the email. This is what the recipient will see as the email’s subject.

$subject = "Enquiry from. $nfrm_name . through our website";

$body is the body content of the email. Ideally you can put in anything here. But in our case we need to send the data from the enquiry form.

$body = <<<EOD
       
        <table cellspacing="0" cellpadding="1" border="1">
            <tbody>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Name: </td>
                    <td style="padding: 5px 10px;">$nfrm_name</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Mobile: </td>
                    <td style="padding: 5px 10px;">$nfrm_mobile</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Email: </td>
                    <td style="padding: 5px 10px;">$nfrm_email</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Message: </td>
                    <td style="padding: 5px 10px;">$nfrm_message</td>
                </tr>
            </tbody>
        </table>
       
EOD
;

$headers are usually required by the email clients. These $headers tell the email client where the email originated from, who sent it, what is the content type of the email and what is the php version that was used to send it. Without all these information the emails will end into the SPAM folder. So make sure you add them. The more information you send the better possibilities that your mail will not be treated as SPAM.

    $headers = "From: website@company.com\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset="iso-8859-1"\n";
    $headers .= "X-Priority: 1\r\n";
    $headers .= "X-MSMail-Priority: High\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n";

Putting all these together we get this code. The final code that will insert data into our mysql database using php and send a mail also.

<?php
    if (isset($_POST['enq_submit'])){
        $nfrm_name = $_POST['enq_name'];
        $nfrm_mobile = $_POST['enq_mobile'];
        $nfrm_email = $_POST['enq_email'];
        $nfrm_message = $_POST['enq_message'];
       
        $sql = "INSERT INTO tbl_enquiry (nfrm_name, nfrm_mobile, nfrm_email, nfrm_message)
                VALUES ('$nfrm_name', '$nfrm_mobile', '$nfrm_email', '$nfrm_message')"
;
        if (!mysql_query($sql,$connection)){
            die('Error: ' . mysql_error());
        }
       
        $emailID = "info@comany.com, ceo@comany.com";
        $subject = "Enquiry from. $nfrm_name . through our website";
$body = <<<EOD
       
        <table cellspacing="0" cellpadding="1" border="1">
            <tbody>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Name: </td>
                    <td style="padding: 5px 10px;">$nfrm_name</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Mobile: </td>
                    <td style="padding: 5px 10px;">$nfrm_mobile</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Email: </td>
                    <td style="padding: 5px 10px;">$nfrm_email</td>
                </tr>
                <tr>
                    <td style="padding: 5px 10px;" width="150">Message: </td>
                    <td style="padding: 5px 10px;">$nfrm_message</td>
                </tr>
            </tbody>
        </table>
       
EOD
;
   
        $headers = "From: website@company.com\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset="iso-8859-1"\n";
        $headers .= "X-Priority: 1\r\n";
        $headers .= "X-MSMail-Priority: High\n";
        $headers .= "X-Mailer: PHP". phpversion() ."\r\n";
       
        mail($emailID, $subject, $body, $headers );
        echo "<h4>Thank you for sending us an enquiry. We will get back to you.</h4>";
       
    } else {
        echo("Kindly use the form to submit the data!");
    };
?>

Hope you enjoyed this little long tutorial. Well it was needed and I believe it was worth the time. Because when you make big bucks with this knowledge you sure should come back and tell us your story. We love hearing them in our comments. Thanking for reading.

Latest Comments

4 responses to “How to insert data into mysql database using PHP form and then send mail – Part 2”

  1. mark thomas says:

    I’m fairly new to php and have spent a week trying to do what you have done in the tutorial.
    Then I found the tutorial, thank god there are people like you, you have covered everything I needed and explained it well enough for a novice to understand.
    Please add this virtual

  2. Anachak says:

    like Mark I had been trying to solve the puzzle of two posts using PDO
    took a little adaptation and your example has an error in it but who cares? I now know how to do it
    thank you Mr. JCRP – you’re a diamond

  3. Diimaan says:

    Hi,

    Thank you very much for this really helpful tutorial. I have one doubt. In my website I am using phpmailer to send mail when the visitors submit the form.
    Is it possible to include the database functionality of mysql in that code?
    It will be of great help if you could assist me on this.
    Thank You 🙂

Leave a Reply

comment on this article