This example shows how to create a new row in a table named Book using the Book class. The Book table has BookID(primary key, auto-increment), Title(string), and Author(string) columns.
require_once('config.php');
//Create new instance of the Book class
$myBook = new Book;
//Set the values of some of the columns
$myBook->setTitle('Hop On Pop');
$myBook->setAuthor('Dr. Seuss');
//Save the column values in a new row in the database
$myBook->save();
//Print the unique id of the row
echo $myBook->getBookID();
?>
In this example, there is no interaction with the database until the save() method is called. The save() method is what actually stores the column values in a row in the database. The first time you call the save() method for a table that has an auto-incremented column(in this case ‘BookID’) without a value, the save() method will insert a new row and that column will automatically be updated with the value created by the database. Once that value has been set, calling the save() method again will attempt to update the existing row, instead of inserting a new one.
(503) 746-9116 | 17933 NW Evergreen Parkway, Suite 220 | Beaverton, Oregon 97006