MySql – Insert

Inserting rows into MySql can be done in a couple of ways. You can use the INSERT…VALUES and INSERT…SET commands.

The first option INSERT…VALUES syntax is:

INSERT INTO `tbl_name` VALUES ('fieldvalue1', ... , 'fieldvalueN');

Example:

INSERT INTO `people` VALUES (NULL, 'Andrews', 'Donald', 'M', '24', ..., '11', 'A+', 'BS3 5TY');

The second option INSERT…SET syntax alternative requires that you specify each field name with its counterpart in the same format as update queries.

`surname` = 'Smiths', 
`firstname` = 'John'

If you are inserting new values in every field for a table then the first option is the most simple. You can always design your system such that it will accept NULL values. If however you only want to insert values into particular fields within a table you could use the second option.

What ID was that field given?

Often after inserting a row you want to know what value that row was given. MySql will return the last insert row with the following php command

mysql_insert_id();

Further Information on INSERT

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.