Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

MySQL / Add rows

  1. Add a row

1. Add a row

After we created the tables with the colums we want to add new rows into the table. This can be done with the INSERT INTO command. After the INSERT INTO keyword you add the table name which you want to fill. After the name there is a list of columns for which you want supplie values. This list is written in parenthese and seperated by commas. If a column is not written there mysql will save a defaut value in this column for the new row. As an example an INT column have the default value 0. After the column names there is the keyword VALUES. Now all values are followed, also seperated with commas and written in parentheses. As in php strings must be written in quotes.

As an example we add a row without any values.

INSERT INTO
    news()
VALUES
    ();

This create add a new row into the news table.

Tabelle News
id author title content added
1 0000-00-00 00:00:00

As you see the id column gets the value 1 automatically. The values for author, title and content are always an empty string. And the added column got the (invalid) time 0000-00-00 00:00:00. As an example we add a real row.

INSERT INTO
    news(author, title, content, added)
VALUES
    ("Me",
     "My first news",
     "I am testing how rows are added to mysql",
    NOW());

You see we only used 4 of 5 columns as the id column got its value by AUTO_INCREMENT. Additional the column added got the value NOW(). This is a mysql function which returns the current time and date. This value is saved in the added column. If the query is executed the table may look like this.

Tabelle News
id author title content added
1 0000-00-00 00:00:00
2 Me My first news I am testing how rows are added to mysql 2008-08-17 14:02:08

This table got 2 rows now.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!