|
Added on 15.11.09 |
| Clicks 2139 |
| Rating: 4.50 out of 5 from 2 raters |

In the previous 2 parts, we have learned how to create a database and a table and now we are about to insert some data in our table.
We have a table called books, therefore ,let us put some books titles in it,as you can see on the following part
| ID | Title | Author | Category | Price |
| 1 | Advanced Database Technology and Design | Mario Piattini and Oscar Diaz | Database | $ 89.00 |
| 2 | Conceptual Database Design | Carol Batini, Shamkant Navathe | Database | $ 96.80 |
| 3 | A Guide to MySQL | Philip J. Pratt | MySql | $ 55.35 |
| 4 | Beginning Databases with MySQL | Richard Stones | MySql | $ 48.90 |
The above code ouputs 4
because we inserted 4 rows at once.We may insert 1 row at the time
but if you need,like in this case to save time ,to insert multiple rows,just
follow this example.We started ,as usual,with the variables
declaration and the we establish a connection to MySql and select
a Database to work with,after that we assign the $sql variable the
sql statement $sql="INSERT INTO books (
title, author, category, price) VALUES ('Advanced Database
Technology and Design', 'Mario Piattini and Oscar Diaz',
'Database','89.00' ),
('Conceptual Database Design', 'Carol Batini
Shamkant Navathe', 'Database', '96.80'), ('A Guide to MySQL' ,'Philip
J.Pratt', 'MySql', '55.35'),
('Beginning Databases with MySQL', 'Richard
Stones', 'MySql', '48.90')";
as you
can see the statements that use VALUES
syntax can insert multiple
rows. To
do this, include multiple lists of column values, each enclosed
within parentheses and separated by commas
INSERT
INTO tbl_name (a,b,c)
VALUES(1,2,3),(4,5,6),(7,8,9);
but to INSERT only 1 row just use
the following
INSERT
INTO tbl_name
((a,b,c) VALUES(1,2,3);
That's it,we have just inserted our 4 rows into the database,in the next tutorial,we'll see how to retrieve these books and manipulate the data.