9.3.6 Inserting dataThis is NOT the latest copy of this book; click here for the latest version.
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 see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|