Close

April 29, 2015

How to Insert Form Data Into a MySQL Database Using PHP

Why won’t my form data insert properly into my MySQL database? Here’s the most common reasons why:

Get the updated source code for this video here.

The two most common issues I see developers having are:

  1. Not connecting to MySQL properly
  2. Not matching form fields to database columns properly

Issues Connecting to MySQL

When you run into connection issues, you’ll usually see an error that will say there was a problem connecting to the database.

More often than not, it’s simply a typo in your database name, username, or password.

Make sure to triple-check that information. Also, keep in mind if you’re using source code I’ve provide you’ll need to change those values to whatever they are for YOUR database.

Issues Matching Form Fields to Database Columns

Almost always, this is caused by a mis-match between your form field names and your database column names.

In your SQL statement, you do the matching directly. Here’s a quick example:

INSERT INTO table_name (database_column_1, database_column_2) VALUES (form_field_1, form_field_2)

So you need to match the form field value to the database column.

I go much more in-depth with this in the video, so be sure to watch it if you’re having this problem.

If you get value from this code snippet, please consider sharing it with another developer or group who could benefit from it.


Also published on Medium.

One Comment on “How to Insert Form Data Into a MySQL Database Using PHP

Carros
April 29, 2018 at 3:01 am

Nice!

by Carros

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *