Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.

MySQL Insert

To be able to do anything with a database, you must first have something in your database!
So how do we do that?

Insert into a MySQL Database: <?PHP

mysql_query("insert into table_name(name,email,address,phone) values('$name','$email','$address','$phone')");

As with all actions relating to a MySQL Databse, we have to use the mysql_query() function.
Anything between the quote marks is what we want to do.

insert into This tells the database that we're inserting a record(s)

table_name(name,email,address,phone) This tells the database into which table we're inserting the record(s), and which fields (or columns) of the table_name we want to fill.

values('$name','$email','$address','$phone') This tells the database to fill the previously stated fields/columns with the following values. This assumes you have PHP variables called $name, $email, $address, & $phone.
The single quotes around each value tells MySQL where each value starts and ends!
If your value is likely to include a single or double quote, make sure you escape them by adding a slash (\).
If the value of the $address variable was 'the smith's house'... mysql would insert 'the smith' and anything after the second single quote would generate an error.
PHP has a dedicated function for doing that: addslashes(). So to add slashes to the variable $address, you would use addslashes($address) which would make the value of $address 'the smith\'s house'. MySQL will understand not to interpret the middle single quote as a start or end point, and will insert the whole value into the database, and will remove the added slash in doing so... so the extra slash won't be stored!!!

Make sure the order of the values match the order of the table_name(name,email,address,phone) fields/columns.

If you want to insert more than one record into the same table at the same name, simply include more than one set of values (wrapped in brackets), separated with a comma: <?PHP

mysql_query("insert into table_name(name,email,address,phone) values('$name','$email','$address','$phone'), ('$name2','$email2','$address2','$phone2'), ('$name3','$email3','$address3','$phone3')");


If you want to insert a record, and every field/column will be filled, you can omit the field/column name: <?PHP

mysql_query("insert into table_name values('$name','$email','$address','$phone')");