Exact PHP Code to Insert Data into MySql Database

How to Safely and Securely Insert Data into a Database Using MySql in a Way the Eliminates MySql Inject Attacks and PHP Code that is the Shortest Most Efficient Code for Inserting Data into MySql Database

Home Short:

Inserting data into a database via mysql is fairly uncomplicated however the most important thing to keep in mind is security. Improperly inserting data via php mysql can result in a mysql injected attack where your database and perhaps your entire website becomes vulnerable and destroyed. With this in mind you want to regularly back up your database and store the backup offsite.

Inserting a new column into a database via php occurs with 3 steps.

  1. Connect to the database and the table where you wish to insert new data.
  2. Insert the new data to the database.
  3. Closing the connection.

To connect to your database via php:

The second step is simplified via 2 function calls and 2 lines of code which are calls to these 2 php function.

Creating the new row for your database via php:

Closing the connection to your database via php:

Use the code struction above to put new data into a database table and inserting new rows into your database using php will be smooth, safe and efficient.

S
H
A
R
E