Here's the solution to the "homework" challenge posed above.
These changes were required to insert a "Delete this Joke" link
next to each joke:
- Previously, we passed an
$addjoke variable with
our "Add a Joke!" link at the bottom of the page to signal that
our script should display the joke entry form, instead of the
usual list of jokes. In a similar fashion, we pass a
$deletejoke variable with our "Delete this Joke"
link to indicate our desire to have a joke deleted.
- For each joke, we fetch the
ID column from the
database, along with the JokeText column, so that we
know which ID is associated with each joke in the database.
- We set the value of the
$deletejoke variable to
the ID of the joke that we're deleting. To do this, we insert the
ID value fetched from the database into the HTML code for the
"Delete this Joke" link of each joke.
- Using an
if statement, we watch to see if
$deletejoke is set to a particular value (through
the isset function) when the page loads. If it is,
we use the value to which it is set (the ID of the joke to be
deleted) in an SQL DELETE statement that deletes the
joke in question.
The complete code for challege.php is available to
download free when you purchase
the book.
If you have any questions, don't hesitate to post them in the
SitePoint.com forums!
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($addjoke)): // If the user wants to add a joke
?>
<form action="<?=$PHP_SELF?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p >" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT") {
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<P>Your joke has been added.</P>");
} else {
echo("<P>Error adding submitted joke: " .
mysql_error() . "</P>");
}
}
// If a joke has been deleted,
// remove it from the database.
if (isset($deletejoke)) {
$sql = "DELETE FROM Jokes
WHERE ID=$deletejoke";
if (@mysql_query($sql)) {
echo("<p>The joke has been deleted.</p>");
} else {
echo("<p>Error deleting joke: " .
mysql_error() . "</p>");
}
}
echo("<p> Here are all the jokes in our database: </p>");
// Request the ID and text of all the jokes
$result = @mysql_query("SELECT ID, JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the text of each joke in a paragraph
// with a "Delete this Joke" link next to each.
while ( $row = mysql_fetch_array($result) ) {
$jokeid = $row["ID"];
$joketext = $row["JokeText"];
echo("<p>$joketext " .
"<a href='$PHP_SELF?deletejoke=$jokeid'>" .
"Delete this Joke</a></p>");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href='$PHP_SELF?addjoke=1'>Add a Joke!</a></p>");
endif;
?>
</body>
</html>