JotForm User Guide / Advanced Features /

How to send Submissions to Your MySQL Database Using PHP

How to send Submissions to Your MySQL Database Using PHP

First thing you'll need to learn is to know how to post submission data to your thank you page.  You can read and learn from the this guide

Assuming that you have set your form to post data to a custom URL page, you will have do some PHP/MySQL programming to do a form to MySQL.

The data being posted by JotForm is actually an array so to find out the array keys and values, you will have to try to insert this in your script:

   print_r( $_POST );   ?>

Next:  Do an actual submission to test your form and get the result.  Once you have submitted the form, it should be redirected to your custom thank-you URL that will receive the post data.  Here's an example of the data from a sample form:  https://www.jotform.com/form/11814245193 

Array
(
    [submission_id] => 186725738203177939
    [formID] => 11814245193
    [ip] => 203.177.93.98
    [name] => sample name test data
    [email] => email@gmail.com
    [phonenumber13] => Array
        (
            [0] => 3343
            [1] => 4234
        )

    [subject7] => Technical Support assistance
    [message6] => test
)

(Feel free to make a submission using this form to see the array result for yourself )

Now that we have the keys, we can now play around with these information, create your own fields on your database.  As for this example, this is what my fields look like:

Table structure

Creating your PHP script

Now that you have created your database, you can start creating your PHP script.

Step 1: To prevent possible SQL injection vulnerabilities to your database, we'll need to run PHP's addslash() function for every post variable.  And since the $_POST data is an array, let's make a function that will loop on every variable:

// This function will run within each post array including multi-dimensional arrays
function ExtendedAddslash(&$params)
{
        foreach ($params as &$var) {
            // check if $var is an array. If yes, it will start another ExtendedAddslash() function to loop to each key inside.
            is_array($var) ? ExtendedAddslash($var) : $var=addslashes($var);
        }
}

     // Initialize ExtendedAddslash() function for every $_POST variable
    ExtendedAddslash($_POST);     

?>

Step 2:  I identified each POST data with a variable.  You will also notice that I merged the two phone number POST variables in to one (I just liked it that way :) ):

$submission_id = $_POST['submission_id'];
$formID =
$_POST['formID'];
$ip =
$_POST['ip'];
$name =
$_POST['name'];
$email =
$_POST['email'];
$phonenumber =
$_POST['phonenumber13'][0] ."-". $_POST['phonenumber13'][1];
$subject =
$_POST['subject7'];
$message =
$_POST['message6'];

?>

Step 3:  Now we initialize the database connectivity for the script to MySQL


$db_host = 'db hostname here';
$db_username = 'db username here';
$db_password = 'db password here';
$db_name = 'name of your database';

mysql_connect( $db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name);
?>

Step 4: To check if an insert or an update should be done, a search should be initialized if the submission ID exists and then set a condition that if the submission ID exists, an update should be initialized using that submission ID.  And if there is no submission ID matching, then a new insert is going to be made:


// search submission ID

$query = "SELECT * FROM `table_name` WHERE `submission_id` = '$submission_id'";
$sqlsearch = mysql_query($query);
$resultcount = mysql_numrows($sqlsearch);

if ($resultcount > 0) {
 
    mysql_query("UPDATE `table_name` SET
                                `name` = '$name',
                                `email` = '$email',
                                `phone` = '$phonenumber',
                                `subject` = '$subject',
                                `message` = '$message'       
                             WHERE `submission_id` = '$submission_id'")
     or die(mysql_error());
   
} else {

    mysql_query("INSERT INTO `table_name` (submission_id, formID, IP,
                                                                          name, email, phone, subject, message)
                               VALUES ('$submission_id', '$formID', '$ip',
                                                 '$name', '$email', '$phonenumber', '$subject', '$message') ")
    or die(mysql_error()); 

}
?>

You're done.  Name the script as your thank you php file and upload it to match the exact detail that you have set in your Custom Thank You URL

Here's the complete script:

// This function will run within each post array including multi-dimensional arrays
function ExtendedAddslash(&$params)
{
        foreach ($params as &$var) {
            // check if $var is an array. If yes, it will start another ExtendedAddslash() function to loop to each key inside.
            is_array($var) ? ExtendedAddslash($var) : $var=addslashes($var);
            unset($var);
        }
}

// Initialize ExtendedAddslash() function for every $_POST variable
ExtendedAddslash($_POST);     

$submission_id = $_POST['submission_id'];
$formID =
$_POST['formID'];
$ip =
$_POST['ip'];
$name =
$_POST['name'];
$email =
$_POST['email'];
$phonenumber =
$_POST['phonenumber13'][0] ."-". $_POST['phonenumber13'][1];
$subject =
$_POST['subject7'];
$message =
$_POST['message6'];

$db_host = 'db hostname here';
$db_username = 'db username here';
$db_password = 'db password here';
$db_name = 'name of your database';


mysql_connect( $db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name);


// search submission ID

$query = "SELECT * FROM `table_name` WHERE `submission_id` = '$submission_id'";
$sqlsearch = mysql_query($query);
$resultcount = mysql_numrows($sqlsearch);

if ($resultcount > 0) {
 
    mysql_query("UPDATE `table_name` SET
                                `name` = '$name',
                                `email` = '$email',
                                `phone` = '$phonenumber',
                                `subject` = '$subject',
                                `message` = '$message'       
                             WHERE `submission_id` = '$submission_id'")
     or die(mysql_error());
   
} else {

    mysql_query("INSERT INTO `table_name` (submission_id, formID, IP,
                                                                          name, email, phone, subject, message)
                               VALUES ('$submission_id', '$formID', '$ip',
                                                 '$name', '$email', '$phonenumber', '$subject', '$message') ")
    or die(mysql_error()); 

}

?>

Send Comment

93 Comments...

See all comments
  • vivek raj

    Great, Thanks For Sharing.

  • Hello

    Thanks.

  • puneet

    good but worst

  • puneet

    good but worst

  • Php On Web

    Hi dude, i have also find out one good example
    Variables – Php Example

  • Rob

    Really nice tutorial thank you

  • Morris

    Thanks

  • chris

    thank you

  • Gede Adi

    Hai, can I have a php code submit data without using tag form ? so like if I click specified button so some data will send to data base (CRUD)

    BEst Regards

  • Oscar Gatheru

    awesome

  • vaishshagun

    i had a query that where we initalize our array??

  • Itunes customer service

    This program has some bugs when the attached the date be to send the MySQL query to show you the error.

  • HNUSK

    Hi,

    I am new to PHP, Please help me ...
    I need to display each student details from database into html form one by one on clicking next button using php.

  • Javed UR Rehman

    This is quite simple and easy tutorial, i also shared a tutorial about how to display data from database into HTML table, i hope you will like it. https://www.allphptricks.com/display-data-from-database-into-html-table-using-php/

  • lalithsagar

    i am new to php actually i have a question. i created a form and oce i submit the form data has to save in my database but data is not saving.can anyone help me with that.





    div {
    background-color: lightgrey;
    width: 300px;
    border: 25px solid green;
    padding: 25px;
    margin: 25px;
    }



  • Modestus

    Hello Chatmate! Can you please assist me with PHP and SQL codes.

    I wannah develop an ITs where i will need to use a form to entering Data in a SQL.

    Best regards

  • Xprobes

    i wonder, i have created a custom post type form for email submission. I was thinking that do they also get saved in any of my wp directory also. The forms are submitted to external post type to external url. Is there any trace submitted email left?

  • Chitti

    Very usefull

  • nani

    good

  • Imran

    Dears,
    Hello I want to create simple website. in website 3 user have been working. one is student, second is teacher and third is principle. when the student is a holiday, he fill the application form and submit to teacher not directly submited to principle. then teacher is checked the application and forward to principle. plz tell me how teacher forward application thorugh php and mysql. i just do not know forward. plz help me. tell me smiple method code or refrence link plz help me.


    Thanks

  • Akash Aher

    awesome article i like it

  • Jan Hovens

    dit is een proef mail

  • karthik

    its very good

  • karthik

    its very good

  • javeriamahveen

    Can I use Oracle database?

  • hhaaee

    plz tell me in easy way

  • omkar

    lets see i have made a poster and on bottom of it i add email, your name and submitt. When i submit it shows the same page. even is we chnge its input name, id for match up. its still not matching

  • anam

    where i have to save php file? because i save my php file in folder Home/username/php/tests... cant match by system .

  • saima

    i need your help in web dewelopment

  • Hamza Y.A

    Weldone it really work

  • Nafiu gide

    Hi
    i have tried to view what a have send as a registration form, but i can't. Please i need your help!
    here are the php codes



    print_form



  • mobotak

    Provides a form submit button.

    Submit buttons are processed the same as regular buttons, except they trigger the form's submit handler.

    Properties:

    #submit: Specifies an alternate callback for form submission when the submit button is pressed. Use '::methodName' format or an array containing the object and method name (for example, [ $this, 'methodName'] ).
    #value: The text to be shown on the button.
    Usage Example:

    $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Save'),
    );
    https://mobotak.com/Categorygrid/id/80/%D8%A8%D8%A7%D8%AA%D8%B1%DB%8C%20%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84

  • rafay Tariq

    awesome article i like it

  • valuchi

    I will like to know if their is any one how can help me redirect script that usually submit in mysql database so it can be submission to email so I can use it on c-panel or shell. I am willing to pay if it can be done.

  • Mugisha isaac

    How do I connect allm that to the HTML form ??

  • Hery

    how can I get information about my app Facebook and to send it to mysql

  • Alemayehu Waza

    Sorry this idea is very complex to understand users and different from other sources it may be give some idea but does not give full idea there try to solve how to insert or retrieve data into or from based on or to the project!.

  • Surendra Bharadwaj

    Mr. XYZ,
    It is a suggestion to change the background of your page.
    And change the visibility of content.

    If someone need help from your website, how he/she will take help.

    There is no match of mackground color and text color in your website......

  • ifin2000

    Hi,
    I need your help.
    when I redirect webhook to https://www.jotform/show-data-post/ there are correct output (array and php code $_POST[...])

    but where I redirect to my php script to catch POST data, array is blank, no data any more.
    print_r($_POST);
    $id_post = $_POST['submission_id'];
    $id_form = $_POST['formID'];
    $ip_addr = $_POST['ip'];
    $nama = $_POST['nama'];
    $mail = $_POST['email'];

    where is the mistake?
    please help me,
    because I had already fallen in love with jotform :)

    thanks

  • shiprequest

    Can you just make a module that transmits everything from Jotform to a MySQL database including the pdf extensions?

  • Hannah

    Hi, good day. Can you make a video of this? So that it will be more clear. Thank you and I'm looking forward to your reply.

  • Rick

    I want to create a form where, for example, a person enters a 7 digit number. Each number represents a different field, although when completing the form the person just records the 7 numbers in the text box. The php form would then pull out the values in the database fields. I don't know if that made sense but if you know what I mean does anyone know of a script/code for it? Many thanks!

  • Francis

    your explanation looks like what i really want to do on my joomla project. but m confused! how do i get this form builder and use it on joomla.
    Thank you.

  • AirGermanWolf

    Where I put the php code?

  • tort

    Its really great helpfull tutorials ,i solved this problem right here HELPFULL TUTORIALS

  • Hello, guys please help. Am learning programming. Have created the php script and a form that should enable me input data to a table in mysql data base but when I run, it literally does not input data but the following message is displayed." Parse error:syntax error, unexpected '$password'(T-VARIABLE)in C:\xammp\htdocs\w\insert.php on line 4.

    Your help will be highly appreciated!

  • Abhinav

    want to connect my php form with data base

  • pktfteam

    how can submission show in app and website

  • tochi

    What did you mean by this please " . You're done. Name the script as your thank you
    php file and upload it to match the exact detail
    that you have set in your Custom Thank You
    URL "

  • Abdulrasheed

    Guys it works but you must correct some things in the above code depending on your database table/fields