This guide shows how to send and save form data to a MySQL database after submission using PHP. A basic understanding of MySQL and PHP is required.
To save submissions to your database
- Create your database and table.
The columns in your table will depend on your form’s structure. See this guide to view your form’s field names or post keys if you’re not sure what columns to add to your database table. - For this guide, ensure to add a “submission_id” column in your table.
In this example form, the table looks as follows in phpMyAdmin:
Note: The process for creating or editing your database will depend on your server setup or web host. Reach out to your provider’s support for assistance.
- Now, download and extract this ZIP file containing the code.
- Open the PHP file in your text editor.
- Search for Database Config in the code and replace the values with your database information.
- Next, search for Data to Save and add the POST data to save in your database.
Use the real_escape_string()
function to prevent possible SQL injection vulnerabilities. To check your form’s POST data, see How to View Submissions as POST Data.
- Search for Queries to Run and edit the UPDATE query. Map the post values to your table columns. For more information, see MySQL UPDATE Statement.
- Do the same with the INSERT query. For more information, see MySQL INSERT Statement.
- Save the PHP file and upload it to your server — take note of the file’s URL.
- Now, set up your form to send data to your PHP file’s URL. See Sending Submission Data via a POST Request for more information.
- Send a test entry to your form and confirm the results.
Send Comment:
137 Comments:
234 days ago
This is elaborate and comprehensible.
253 days ago
Let me try it
More than a year ago
Hello,
Can we send post and also direct users to thank you page? Why disable thank you page? is it just to show the post message?
More than a year ago
php script cant find?
More than a year ago
I have done everything right by following a YouTube video, I was taught how to create a form and send it to a data base(XAMPP). the video coding worked but my coding has not work. after completion of the job. when i click on create account, the information are not save but it returned the php code back to me. And the information are not found in the database.
More than a year ago
Hello. I can successfully insert submission data in my mysql table using mysqli. However, I cannot update data in my table when I edit submissions. Can anyone help me?
More than a year ago
I have a database with the column names as id(Auto Increament), name and address.
i want the query that it can update or insert using the same button.
https:/www.jotformcom/form/90867582341161
More than a year ago
I have created my form and my database. now I don't know where is the form php script.?
should I create it? or it is already exists?
If I should create it, where should I do that? in what platform?
Thank you
More than a year ago
Thanks! I will do it soon!
More than a year ago
Start XAMPP Server.
Open localhost/phpmyadmin in your web browser.
Create database of name staff and table of name college.
Write HTML and PHP code in your Notepad in a particular folder.
Submit data through HTML Form.
Verify the results.
More than a year ago
@ce4atomizer
The mysql_connect() function was removed in PHP7. Use the mysqli_connect() function instead. Docs:
More than a year ago
now the only problem is http 500 and
Stack trace:
#0 {main}
thrown in /home/topwax/public_html/index.php on line 25
[11-May-2021 07:11:46 UTC] PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/topwax/public_html/index.php:25
Stack trace:
#0 {main}
thrown in /home/topwax/public_html/index.php on line 25
this is the line 25: mysql_connect( $db_host, $db_username, $db_password) or die(mysql_error());
More than a year ago
Hello, It's saying PHP Parse error: syntax error, unexpected 'arrays' (T_STRING) in /home/topwax/public_html/index.php on line 4 and http 500 error, please help
More than a year ago
Hello. I got some trouble in the integration Jotform Submission to MySQL, i'm using php 7 but the complete script in tutorial "How to send Submissions to Your MySQL Database Using PHP" is doesn't work for me ):
Please help me to fix it, here is my script code :
More than a year ago
Hi, This is exactly what I've been looking for .... my problem is that I am not a programmer, just a novice with a little project that I import 25 entries quarterly in my database. It sure would be much better to have it update with each submission rather imported all at once... And apparently since I have PHP version: 7.3.6 , some of the code must be updated, especially related to mysql to mysqli , and some others... Has this already been done somewhere ? If not I haven't been able to find it ... or could someone do it for me. Thanks in Advance, Steve
More than a year ago
I have a database with the column names as id(Auto Increament), name and address.
i want the query that it can update or insert using the same button.
it also has a search button, but i am not able to search it. could you please help me
thank you
More than a year ago
I am unable to figure out how to enter data into a form and then query mysql with the form entries and output the results:
e.g.
table_name=work
columns include but are not limited to labor, day, hours, etc.
form includes entry for 1. Drivers: trucks, cars, fork lifts; 2. Days: mon, tues, wed... ; Hours: day, afternoons, nights.
If I make entries into the form 1. trucks, 2: wed, 3: day then my query resembles SELECT * FROM work WHERE drivers = trucks AND days = mon AND hours = days then I want to enter the form data into a mysqli query and output which drivers drove a truck on the day shift on Monday.
Been pulling my hair out trying to get this to work.
More than a year ago
I am unable to figure out how to enter data into a form and then query mysql with the form entries and output the results:
e.g.
table_name=work
columns include but are not limited to labor, day, hours, etc.
form includes entry for 1. Drivers: trucks, cars, fork lifts; 2. Days: mon, tues, wed... ; Hours: day, afternoons, nights.
If I make entries into the form 1. trucks, 2: wed, 3: day then my query resembles SELECT * FROM work WHERE drivers = trucks AND days = mon AND hours = days then I want to enter the form data into a mysqli query and output which drivers drove a truck on the day shift on Monday.
Been pulling my hair out trying to get this to work.
More than a year ago
how to insert user IP address in table database?
More than a year ago
Ii wanna know if im registered on the Data base
More than a year ago
so how i can insert data ?
More than a year ago
Can this script be modified to post form data into more than one table?
More than a year ago
Thank you! this was great and helped me finish my marketing project on time. Appreciate all the awesome examples!.
Regards,
More than a year ago
Just thought I'd mention, you're giving bad advice on how to solve SQL injection. Adding slashes is not going to work properly if the person is not using MySQL, which is why the php documentation for addslashes() specifically states that is bad advice and suggests that you use a database-specific method to solve it.
Instead a proper suggestion to tell the person wanting to save your data is to use prepared statements:
If you're using PHP, this can be accomplished via PDO or PEAR, or for MySQL specifically, you can use mysqli.
Using backslashes in SQL code can introduce a whole host of other problems.
From the same stackoverflow article:
For example, there(1) are(2) still(3) many(4) answers(5), including the second most upvoted answer suggesting you manual string escaping - an outdated approach that is proven to be insecure.
More than a year ago
How do you get the array to include ALL data fields even when the form is set to skip/hide fields based on certain responses?
The problem I'm having is that I want the form to be interactive, but the results feed keeps changing. Sometimes it doesn't include fields altogether, other times it does but with a NULL value (which is fine), and yet other times still, the order is all jumbled up.
Obviously this causes the sql insert to fail because of mismatched data types or missing values altogether.
Please help!
Thanks.