13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implementing Bookmark Storage <strong>and</strong> Retrieval<br />

601<br />

Listing 27.24<br />

Continued<br />

}<br />

// get the bookmarks this user has saved<br />

if ($url_array = get_user_urls($valid_user)) {<br />

display_user_urls($url_array);<br />

}<br />

display_user_menu();<br />

do_html_footer();<br />

?><br />

You begin this script by performing the usual validations.When you know that the user<br />

has selected some bookmarks for deletion, you delete them in the following loop:<br />

foreach($del_me as $url) {<br />

if (delete_bm($valid_user, $url)) {<br />

echo 'Deleted '.htmlspecialchars($url).'.';<br />

} else {<br />

echo 'Could not delete '.htmlspecialchars($url).'.';<br />

}<br />

}<br />

As you can see, the delete_bm() function does the actual work of deleting the bookmark<br />

from the database.This function is shown in Listing 27.25.<br />

Listing 27.25 delete_bm()Function in url_fns.php—This Function Deletes a<br />

Single Bookmark from a User’s List<br />

function delete_bm($user, $url) {<br />

// delete one URL from the database<br />

$conn = db_connect();<br />

// delete the bookmark<br />

if (!$conn->query("delete from bookmark where<br />

username='".$user."' <strong>and</strong> bm_url='".$url."'")) {<br />

throw new Exception('Bookmark could not be deleted');<br />

}<br />

return true;<br />

}<br />

As you can see, delete_bm() is also a pretty simple function. It attempts to delete the<br />

bookmark for a particular user from the database. Note that you want to remove a particular<br />

username-bookmark pair in this case. Other users might still have this URL<br />

bookmarked.<br />

Some sample output from running the deletion script on the system is shown in<br />

Figure 27.10.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!