. .... Internet marketing resources, ecommerce web site design tutorials and  just for fun - free cell phone ringtones!
  Taming the Beast - quality web marketing and ecommerce development services .... .

.

One of the worlds most popular PHP/MySQL tutorial manuals - fully revised edition!
Learning PHP doesn't have to be difficult if you have a great manual - read a free chapter from 
"Build Your Own Database Driven Website Using PHP & MySQL". This manual is full of great PHP 
and MySQL development tutorials that will have you creating your first web project quickly & easily.

 

Click here to return to Taming the Beast's Home Page

MySQL & PHP Manual - Build your own database driven web site!

Finally, a practical, down-to-earth guide to learning PHP & MySQL!

We hope you enjoy this sample chapter from "Build Your Own Database Driven Website Using PHP & MySQL" This book is only available from the SitePoint Website for US$34.95. It also comes with electronic access to all PHP and MySQL code samples used throughout the book. You can also download 4 full chapters from the manual for free from the site.

 

Download free chapters from this book, click here

Page - 1 - 2 - 3 - 4 - 5 - 6

You are on page 6

Click here for previous page....

"Homework" Solution

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.

PHP-MySQL book - download free chapters

Build Your Own Database Driven Website Using PHP & MySQL" 4 Chapters (over 100 pages) that you can download for free!

PHP developer manual - free sample download ‘ Practical Solutions to common problems ’  PHP Anthology is a complete guide for any PHP developer Download free manual chapters


<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>

End of chapter return to:

Page - 1 - 2 - 3 - 4 - 5 - 6

PHP-MySQL book - download free chapters

Build Your Own Database Driven Website Using PHP & MySQL" 4 Chapters (over 100 pages) that you can download for free!

PHP developer manual - free sample download ‘ Practical Solutions to common problems ’  PHP Anthology is a complete guide for any PHP developer Download free manual chapters

 

 

Home

TTB is powered by renewable energy our office is powered by:
Learn more about our social and environmental commitment

 

Return to top of page 

Get paid cash taking online surveys - free to join online 
survey companies that will pay you cash for your opinion!

In Loving Memory - Mignon Ann Bloch

copyright (c) 1999-2007  Taming the Beast  Adelaide - South Australia 

Profile - Contact - Privacy - Advertise - Site Resources - Consultants Portfolio 

Search Site - Terms of Service - Usability Issues