MANAGING CONTENT // create database (Web hosting domain names) connection $conn =
Tuesday, June 10th, 2008MANAGING CONTENT // create database connection $conn = dbConnect(’admin’); // get details of selected record if (isset($_GET[’article_id’]) && !$_POST) { // prepare SQL query $sql = ‘SELECT article_id, title, article FROM journal WHERE article_id = ?’; // initialize statement $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { // bind the query parameters $stmt->bind_param(’i', $_GET[’article_id’]); // bind the results to variables $stmt->bind_result($article_id, $title, $article); // execute the query, and fetch the result $OK = $stmt->execute(); $stmt->fetch(); } } // redirect if $_GET[’article_id’] not defined if (!isset($_GET[’article_id’])) { header(’Location: http://localhost/phpsolutions/admin/ . journal_list.php’); exit; } // display error message if query fails if (isset($stmt) && !$OK && !$done) { echo $stmt->error; } ?> Although this is very similar to the code used for the insert page, the first few lines are outside the conditional statements. Both stages of the update process require the include files, the removal of backslashes, and the database connection, so this avoids the need to duplicate the same code later. Two flags are initialized: $OK to check the success of retrieving the record, and $done to check whether the update succeeds. The first conditional statement makes sure that $_GET[’article_id’] exists and that the $_POST array is empty. So the code inside the braces is executed only when the query string is set, but the form hasn t been submitted. You prepare the SELECT query in the same way as for an INSERT command, using a question mark as a placeholder for the variable. However, note that instead of using an asterisk to retrieve all columns, the query specifies three columns by name like this: $sql = ‘SELECT article_id, title, article FROM journal WHERE article_id = ?’; 365
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.