Creating the Edit Author Page

Một phần của tài liệu Tài liệu Learning PHP Data Objects ppt (Trang 75 - 80)

Our application still lacks the add/edit author functionality. This page will be somewhat simpler than the edit book page because it will not have the select box for authors and no unique index. (You may want to create a unique index on the author's first and last name columns to prevent duplicates there too, but we will leave this up to you.)

Let's call this page editAuthor.php. Here is its source code:

<?php /**

* This page allows to add or edit an author * PDO Library Management example application

* @author Dennis Popel */

// Don't forget the include include('common.inc.php');

// See if we have the author ID passed in the request

$id = (int)$_REQUEST['author'];

if($id) {

// We have the ID, get the author details from the table $q = $conn->query("SELECT * FROM authors WHERE id=$id");

$author = $q->fetch(PDO::FETCH_ASSOC);

$q->closeCursor();

$q = null;

} else {

// We are creating a new book $author = array();

}

// Now see if the form was submitted if($_POST['submit']) {

// Validate every field $warnings = array();

// First name should be non-empty if(!$_POST['firstName']) {

$warnings[] = 'Please enter first name';

}

// Last name should be non-empty if(!$_POST['lastName']) {

$warnings[] = 'Please enter last name';

}

// Bio should be non-empty if(!$_POST['bio']) {

$warnings[] = 'Please enter bio';

}

// If there are no errors, we can update the database // If there was book ID passed, update that book if(count($warnings) == 0) {

if(@$author['id']) {

$sql = "UPDATE authors SET firstName=" . $co>quote($_POST['firstName']) .

', lastName=' . $conn->quote($_POST['lastName']) . ', bio=' . $conn->quote($_POST['bio']) .

" WHERE id=$author[id]";

} else {

$sql = "INSERT INTO authors(firstName, lastName, bio) VALUES(" . $conn->quote($_POST['firstName']) .

', ' . $conn->quote($_POST['lastName']) . ', ' . $conn->quote($_POST['bio']) . ')';

}

$conn->query($sql);

header("Location: authors.php");

exit;

} } else {

// Form was not submitted.

// Populate the $_POST array with the author's details $_POST = $author;

}

// Display the header showHeader('Edit Author');

// If we have any warnings, display them now if(count($warnings)) {

echo "<b>Please correct these errors:</b><br>";

foreach($warnings as $w) {

echo "- ", htmlspecialchars($w), "<br>";

} }

// Now display the form

?>

<form action="editAuthor.php" method="post">

<table border="1" cellpadding="3">

<tr>

<td>First name</td>

<td>

<input type="text" name="firstName"

value="<?=htmlspecialchars($_POST['firstName'])?>">

</td>

</tr>

<tr>

<td>Last name</td>

<td>

<input type="text" name="lastName"

value="<?=htmlspecialchars($_POST['lastName'])?>">

</td>

</tr>

<tr>

<td>Bio</td>

<td>

<textarea name="bio"><?=htmlspecialchars($_POST['bio'])?>

</textarea>

</td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" name="submit" value="Save">

</td>

</tr>

</table>

<?php if(@$author['id']) { ?>

<input type="hidden" name="author" value="<?=$author['id']?>">

<?php } ?>

</form>

<?php

// Display footer showFooter();

This source is built in the same way as the editBook.php page so you should be able to follow it easily.

We will link to the editAuthors.php page in the same way as we linked to the editBook.php page from the books.php page. Edit the authors.php file and change lines 30-41 to the following:

while($r = $q->fetch(PDO::FETCH_ASSOC)) {

?>

<tr>

<td><?=htmlspecialchars($r['firstName'])?></td>

<td><?=htmlspecialchars($r['lastName'])?></td>

<td><?=htmlspecialchars($r['bio'])?></td>

<td>

<a href="editAuthor.php?author=<?=$r['id']?>">Edit</a>

</td>

</tr>

<?php }

Add the following line just before the last PHP block:

<a href="editAuthor.php">Add Author...</a>

Now, if you refresh the authors.php page you will see the following:

You can click the Edit links in the rightmost column to edit every author's details.

You can try submitting the form with empty values to see that invalid submissions will be rejected. Also, you can try and add a new author to the system. After you successfully do this, you may want to go back to books listing and edit some book.

You will see that newly created author is available in the authors select box.

Một phần của tài liệu Tài liệu Learning PHP Data Objects ppt (Trang 75 - 80)

Tải bản đầy đủ (PDF)

(188 trang)