By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Inserting Rows in the MySQL Table

Working with Databases is actually easy. This Tutorial shows how to Insert Rows, in a MySQL Database.

Edited: 2015-07-10 16:41

Your next step is to Insert some rows in your newly created table, this is one of the easiest things to do. Be sure to validate input from forms before you insert it however, because otherwise you will be wide open to injection attacks and the like.

I will cover the Markup and Style of the Guestbook in a later Article.

Inserting Posts in the Table

The below Example shows how to Insert Data in your MySQL table.

INSERT INTO GuestBook_Posts (Text, Time) VALUES (
    'This is some Example Text', 

The below shows how to do it in PHP.


$TimeStamp = time();

mysql_query("INSERT INTO GuestBook_Posts (Text, Time) VALUES (
    'This is some Example Text', 
    '$TimeStamp')", $Connection) or die(mysql_error());


You can use the PHP time to get te current time, like done in above Example.


This time i recommend that you read the Explanation.

The Insert

How the Insert Works.

INSERT INTO GuestBook_Posts (Text, Time)

The above part tells MySQL where to insert the Data, in this case we will be inserting in the GuestBook_Posts Table, and the Text and Time columns.

    'This is some Example Text', 
    '1241015253')", $Connection)

The above is what to be Inserted, and the connection to be used. Note that they are ordered in the same way as we defined where to insert the data (Text, Time)

The wrong way of doing this would be like below.

    'This is some Example Text')", $Connection)

The last part just outputs an error, (if any).

or die(mysql_error());

Is the PostID Missing?

PostID is only missing from our query because its the Auto Increment column, MySQL will fill it out on its own, so we do not need to worry about finding a Unique ID.