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

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

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

You are on Page 4

Click here for previous page...

We now have the code that will allow a user to type a joke and add it to our database. All that remains is to slot it into our existing joke viewing page in a useful fashion. Since most users will only want to view our jokes, we don't want to mar our page with a big, ugly form unless the user expresses an interest in adding a new joke. For this reason, our application is well suited for implementation as a multi-purpose page. Here's the code (available as jokes.php in the http://sitepoint.com/books/?bookid=MoreBooks code archive):

<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: // Default page display
// 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>");
}
}

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>

There we go! With a single file that contains a little PHP code we're able to view existing jokes, and add jokes to, our MySQL database.


Read on...

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