Inserting data

Now that you are able to create, alter, and delete tables at will, we can continue on by looking at how to insert data into our test table. Make sure you have the phpdb database and the original usertable table (that is, with Age and without MiddleName) created before continuing.

To insert data into your table, you use the "INSERT INTO" SQL command:

INSERT INTO usertable VALUES (1, "Jack", "Black", 29);

The INSERT INTO command above tells your database that you would like your data placed into the specified table - usertable, in the example. The data in between the brackets themselves is what goes into your fields, and it should match with the table definition precisely. As usertable had the fields ID INT, FirstName CHAR(255), LastName CHAR(255), and Age INT, the values we're entering are ID 1, FirstName "Jack", LastName "Black", and "Age 29. Note that the character data must be surrounded by quotes (either single or double).

We'll be looking at more advanced data insertion later - for now, it is just important to learn the basics.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Selecting data >>

Previous chapter: Deleting tables

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.