How to send Submissions to Your MySQL Database Using PHP

Last Update: May 21, 2014

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:  http://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:



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);
            unset($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()); 

}

?>

You have already voted for this item. Your vote was: 1

32 Comments...

schef - November 13, 2011 at 04:37 PM    

I use your form to attend my customer my seminars. I am not web developer is it possible to ad this script to my web site easily without code etc.

My email is s_mastorakos@hotmail.com

View Answer

gappsmasters - January 02, 2012 at 06:14 AM    

Is it possible to get someone at JotForm to help us bridge via API to Solve360 CRM? We're wanting to stick with Jotform, but may have to switch to other form builders for integration ease of use.

See here: http://norada.com/?uri=norada/training

I feel confident that someone at JotForm can solve this fairly easily... We just need to POST variables into a PHP script/wrapper right?

View Answer

bappy - January 09, 2012 at 12:41 AM    

How to insert id into database after deleting previous one

View Answer

Alexx123 - January 27, 2012 at 08:30 PM    

OlA , eu entrei no meu jotfrorm e passei pra amigos quando eles enviao algo ele falam que aparece Form Disabled

This form has been disabled

entao eu queria uma ajuda rapida

View Answer

foodorders - March 13, 2012 at 05:39 PM    

Getting an error upon submit:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', '', 'Yes', '', 'Yes', '', 'Yes', '')' at line 4


Here is what I have from line 1 - 12 in my php page:

View Answer

grassmedia - May 10, 2012 at 05:53 AM    

Thanks so much for this, works fine and got me kick started!

rexjimtw - June 27, 2012 at 05:21 AM    

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /raid/vhost/guan-xi.com.tw/www/trymeok.php on line 43
Table 'vhost59811.table_name' doesn't exist


How should I do with this.

View Answer

infoges - July 12, 2012 at 10:08 AM    

Hi,
My database is in OVH, how can I do to link my forms to my database?
thank you to yours answers

View Answer

guest_22721662966056 - September 29, 2012 at 06:49 AM    

Hi
I would like to created a submission form that is also able to 'send' the image,and some of the information on the form,directly to a page on my website so that it is displayed immediately(as well as saving everything to a database).

How is this achieved? Is this something that you can help with?

Cheers.
MJC

View Answer

jacknab - October 12, 2012 at 05:37 AM    

hello could i pay you to build my form of 10 fields to do the same = insert the data into mysql database.

View Answer

Nyameko Yabo - October 15, 2012 at 08:54 AM    

how do you retrieve a form with the information in it and updating the information on the database using php

View Answer

estvmachine - October 31, 2012 at 01:52 PM    

Its works!, thank you. You save my job!.

amit kumar - November 02, 2012 at 02:56 AM    

yu no programi data base ighj df

yuyun - November 16, 2012 at 01:49 AM    

how to insert the data that have the same id with data in the database.

for example, i want to insert the data of branches from the customer master. so, it will send the customermasterId('8') and branchesId ('14') to the database.

however, in the database, it is already have a data with customermasterId ('8')

it means, how can i insert the data with multiple customermasterid. for your information, those id is not a foreign key.

View Answer

yuyun - November 16, 2012 at 01:50 AM    

sorry, i mean it is not a primary key.

sujeet - November 26, 2012 at 04:58 AM    

my dear sir if you know joomla plz clear my doubet

1> how can made registration form in joomla ?
2> how can add dynamic template page in inner page plz explain details and easy langauge

View Answer

Dir3t - December 06, 2012 at 08:02 AM    

Hello I created a form in here just jot form I tried to insert it for my school project where I wanted to implement the fumulário that I created with my SQL database in php.

I tried changing the script but without success, I am working on xampp but am not succeeding.

In my SQL table with the name (user has the following data:
login
pw
email
name

My database is called: headsport, host: localhost, name: root, and no password pw.

Note: on my registration form only has this login, pw, email, name.

Will anyone be able to adapt the script for my project?

Hugs, Dir3t

View Answer

Xavier - April 12, 2013 at 09:45 AM    

Hello,

I do have a concern as sometime jotform add some figures after the field I created.

Let say I add an input field name. On the Post I get on my page, the field that correspond to name is [name17]. Which of course cannot be inserted in my Database.

Of course in my landing page, I could make a script in order to recover the corresponding field but that would not always work

Do we have a possibility to put another tag for the input field (i want my label = name and the post id will be 'username')

View Answer

embscareers - April 26, 2013 at 05:02 AM    

Hello,

Thanks for the script. It works as charm for me. The only one problem I have is that when I include the item "file" for the submitted document. The thank you page shows me that : Unknown column 'file' in 'field list'

That's the code I have:


View Answer

Iwan Saputa - September 12, 2013 at 03:03 AM    

Thank you, finally i found your tutorial was work for me after spend 6 hours just looking for update mysql.

selin - September 13, 2013 at 06:10 AM    

I need to update my entries in my login page to my database i need coding help

View Answer

sportsaac - October 29, 2013 at 05:16 PM    

Getting the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''lacrosse_test' (submission_id, formID, ip, fullname3, email4, newto) VALUE' at line 1

Any ideas?

View Answer

Antonio - November 23, 2013 at 03:13 PM    

Jotform folks

I just copy and the example provided and change all the parameters/string with my database information, but the following message has displayed:

Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in line 33
Below is the 33 in my file. "$resultcount = mysql_numrows($sqlsearch);
"
Any help is greatly appreciated.

Thanks

View Answer

dgkg - January 20, 2014 at 02:40 AM    

So now we can write the fields to a database, but now we need to read it back also and place it onto the page, do you have an example like above to show me how this is done?

View Answer

donadioj - January 25, 2014 at 04:12 AM    

This is great, but how could I run a search on my database and prepopulate form input with data that already corresponds to an id before updating.

View Answer

tunji - January 25, 2014 at 04:52 PM    

hi,i try to run the form to mysql database and it was successful but i cant found the input inside the database.it also show me undefined variable in line 33.here is my codes

View Answer

texico - February 04, 2014 at 04:53 PM    

Nice explanation, what a pity that PHP is not really easy to learn.

Akabuo - February 24, 2014 at 06:00 PM    

very nice

dalepotter_upreach - March 21, 2014 at 06:14 AM    

Thanks for this tutorial, really helped us with lots of form inputting!

By the way, everyone should be using the 'mysqli_query' method to connect to MySQL databases as 'mysql_query' will be removed from PHP in future: http://uk3.php.net/manual/en/function.mysql-query.php

Hi