“Homework” Solution
Here'sthe 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 $_GET['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 $_GET['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.
Here's the complete code, which is also available as challege.php in
the code archive. If you have any questions, don't hesitate to post them in
the SitePoint Forums!
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_GET['addjoke'])): // If the user wants to add a joke
?>
<form action="<?=$_SERVER['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: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'mypasswd');
if (!$dbcnx) {
die( '<p>Unable to connect to the ' .
'database server at this time.</p>' );
}
// Select the jokes database
if (! @mysql_select_db('jokes') ) {
die( '<p>Unable to locate the joke ' .
'database at this time.</p>' );
}
// If a joke has been submitted,
// add it to the database.
if (isset($_POST['submitjoke'])) {
$joketext = $_POST['joketext'];
$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($_GET['deletejoke'])) {
$jokeid = $_GET['deletejoke'];
$sql = "DELETE FROM Jokes
WHERE ID=$jokeid";
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) {
die('<p>Error performing query: ' .
mysql_error() . '</p>');
}
// 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="' . $_SERVER['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="' . $_SERVER['PHP_SELF'] .
'?addjoke=1">Add a Joke!</a></p>');
endif;
?>
</body>
</html>