MANAGING CONTENT That completes the insert page, but (Web hosting top)
Saturday, May 31st, 2008MANAGING CONTENT That completes the insert page, but before testing it, create journal_list.php, which is described in PHP Solution 13-4. PHP Solution 13-3: Inserting a new record with PDO Use journal_insert01.php fromthe download files. The finished code is in journal_insert_pdo.php. 1. The code that inserts a new record should be run only if the form has been submitted, so it s enclosed in a conditional statement that checks for the name attribute of the submit button (insert) in the $_POST array. Put the following above the DOCTYPE declaration: After including the PDO connection function and the file that contains nukeMagicQuotes(), the code removes backslashes from the $_POST array and sets $OK to false. The five comments at the end map out the remaining steps. 2. The first stage in creating a prepared statement is to build a SQL query with placeholders for the data that will be derived from variables. Create a connection to the database as the administrative user (psadmin), and build the SQL like this: // create database connection $conn = dbConnect(’admin’); // create SQL $sql = ‘INSERT INTO journal (title, article, created) VALUES(:title, :article, NOW())’; The values that will be derived from variables are represented by named placeholders consisting of the column name preceded by a colon (:title and :article). The value for the created column is a MySQL function, NOW(), which generates a current timestamp. In the update query later, this column remains untouched, preserving the original date and time. 355
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.