<HTML>
...
<BODY>
<?php
// If the user wants to add a joke
if (isset($addjoke)):
?>
<FORM ACTION="<?php echo($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">
</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 ("SUBMIT" == $submitjoke) {
$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>");
}
}
echo("<P> Here are all the jokes " .
"in our database: </P>");
// Request the text of all the jokes
$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["JokeText"] . "</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>
這是我們上面提出的“家庭作業”的解決方案。要在每一個笑話後面添加一個“Delete this Joke”串連,必須作下面的改動:
之前,我們曾經在我們的頁面的底端的“Add a Joke!”串連中傳遞過一個$addjoke變數,通過這個變數來通知我們的指令碼不再顯示通常的笑話列表,而是顯示一個錄入笑話的表單。與此相類似,我們在我們的“Delete this Joke”串連中傳遞一個$deletejoke變數來表示我們想要刪除一個笑話。
// 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 ("SUBMIT" == $submitjoke) {
$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>