235 lines
14 KiB
HTML
235 lines
14 KiB
HTML
<!--startcut ==============================================-->
|
|
<!-- *** BEGIN HTML header *** -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML><HEAD>
|
|
<title>Developing Web Applications - Part III LG #50</title>
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
|
|
ALINK="#FF0000">
|
|
<!-- *** END HTML header *** -->
|
|
|
|
<!-- *** BEGIN navbar *** -->
|
|
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
|
|
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
|
|
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
|
|
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="rogers.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="../faq/index.html"><IMG ALT="[ Linux Gazette FAQ ]"
|
|
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="silva2.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
|
|
<!-- *** END navbar *** -->
|
|
|
|
<!--endcut ============================================================-->
|
|
|
|
<H4>
|
|
"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
|
|
<P> <HR> <P>
|
|
<!--===================================================================-->
|
|
|
|
<center>
|
|
<H1><font color="maroon">Developing Web Applications - Part III</font></H1>
|
|
<H4>By <a href="mailto:afsilva@liberty.edu">Anderson Silva</a></H4>
|
|
</center>
|
|
<P> <HR> <P>
|
|
|
|
<!-- END header -->
|
|
|
|
|
|
|
|
|
|
<br> At this time, I will close the <b>Developing Web
|
|
Applications</b> series with a very helpful example that if you understand
|
|
it, you will be able to apply the same type of application to several other
|
|
types of online applications. I am talking about <b>creating your own online
|
|
bookmark</b>. Once you understand this example, you will be able to do
|
|
basic <b>mySQL</b> operations with php3.
|
|
<p> But before I get to the php3 code for the bookmark application,
|
|
you will need to create a mysql table to store your bookmarks.
|
|
There are several ways to administrate mySQL databases:
|
|
<ul>
|
|
<li>
|
|
Command Line: You can create all your tables, insert data, and query them
|
|
out from the mysql client. To do this, I would suggest you read the mySQL
|
|
Language reference at <a href=http://www.mysql.com>http://www.mysql.com</a></li>
|
|
|
|
<li>
|
|
GUI Based: You can download several different types of graphical interface
|
|
to administrator a mysql database. For example: xmysql and kmysql. To download
|
|
this tools, I would suggest: <a href=http://www.tuxfinder.com>http://www.tuxfinder.com</a></li>
|
|
|
|
<li>
|
|
Online Interface: This is definitely my favorite option. There is a very
|
|
nice tool called phpMyAdmin, which allows you to administrate one
|
|
or more mySQL database remotely through your browser. Here is the URL:
|
|
<a href=http://www.phpwizard.net/phpMyAdmin>http://www.phpwizard.net/phpMyAdmin</a></li>
|
|
</ul>
|
|
Choose whatever fits you better. For this small project,
|
|
I will give you the configuration that fits the needs for this application.
|
|
<p> Database Host: <b>myserver</b>
|
|
<br> Username: <b>myusername</b>
|
|
<br><b> </b>Password: <b>mypassword</b>
|
|
<br><b> </b>Database Name: <b> mydatabase</b>
|
|
<br><b> </b>Table Name: <b>bookmark</b>
|
|
<br> Fields in the <b>bookmark</b> table: <b>id,
|
|
url, description</b>
|
|
<p> All the information above is relevant when
|
|
coding the application. <b>Note:</b> The fields are the columns on the
|
|
database. The id was defined to allow every entry in your database to be
|
|
unique (<b>primary key</b>), it should be defined to be unique, and auto-increment.
|
|
<p> Once you have your database defined and working,
|
|
you may start coding your application, and here is how it goes:
|
|
<br>
|
|
<p> The HTML form that will capture the data and send
|
|
it in to the database:
|
|
[<A HREF="misc/silva/book_form.html.txt">text copy of this listing</A>]
|
|
|
|
<pre>
|
|
<HTML>
|
|
<head>
|
|
<title>Anderson's bookmark</title>
|
|
</head>
|
|
<body bgcolor=white>
|
|
<form ACTION="sendbook.php3" METHOD="Post">
|
|
<center><p>Enter The Bookmark Title:</font>
|
|
|
|
<input TYPE="text" SIZE="40" NAME="description"> </p>
|
|
|
|
<center><p>Enter The Bookmark URL:</font>
|
|
|
|
<input TYPE="text" SIZE="40" NAME="URL"> </p></center>
|
|
|
|
<p><input TYPE="Submit" VALUE="Check"><br>
|
|
</form>
|
|
<a href="book.php3">View Bookmarks</a>
|
|
</body>
|
|
</html>
|
|
</pre>
|
|
|
|
|
|
The above form will have to text fields: one for the
|
|
URL, the other for the URL description. The <b>form </b>tag will be responsable
|
|
for telling the browser what to do when the <b>Submit</b> button is pressed.
|
|
In this case it will call the php3 script <b>sendbook.php3</b>, and send
|
|
the data to that script.
|
|
<p> The following script is the <b>sendbook.php3</b>.
|
|
This script will open a connection to the mySQL database, and send the
|
|
data from the HTML form to the database.
|
|
[<A HREF="misc/silva/sendbook.php3.txt">text copy</A>]
|
|
|
|
<pre>
|
|
<?php
|
|
//if any of the two fields is left blank, don't send data, but send an error message.
|
|
if(!($description=="") || !($URL==""))
|
|
{
|
|
|
|
//connects to database server
|
|
mysql_connect(myserver, myusername, mypassword);
|
|
|
|
//connects to the database
|
|
mysql_select_db('mydatabase');
|
|
|
|
//this is the query command to insert into the bookmark
|
|
// table the values from $description and $URL
|
|
// inside the Columns description and URL
|
|
mysql_query("insert into bookmark(description, URL) values ('$description', '$URL')");
|
|
|
|
//closes connection to database.
|
|
mysql_close();
|
|
|
|
//After the data is inserted, the browser will form a web page with
|
|
// the following information.
|
|
echo "Thanks for adding the bookmark<br>";
|
|
echo "<a href=book.php3>View BookMraks</a><br>";
|
|
echo "<a href=sendbook.html>Add Another One</a><br>";
|
|
}else{
|
|
echo "You need to go to the form: <a href=sendbook.html>Sendbook</a>";
|
|
}
|
|
?>
|
|
</pre>
|
|
|
|
The third and last script is called <b>book.php3</b>.
|
|
This script will query the data entered by sendbook.php3, and display on
|
|
the screen all of your bookmarks.
|
|
[<A HREF="misc/silva/book.php3.txt">text copy</A>]
|
|
|
|
<pre>
|
|
<? echo "<HTML>";
|
|
echo "<HEAD><TITLE>Afsilva's Bookmark</title></head>";
|
|
echo "<body bgcolor=white>";
|
|
echo "<IMG SRC=bookmark.jpg><br><br>";
|
|
</pre>
|
|
|
|
<pre>
|
|
//Connect to DB server
|
|
mysql_connect(myserver, myusername, mypasword);
|
|
//Connect to Database
|
|
mysql_select_db("mydatabase");
|
|
//Query the database for everything(*) that is on it.
|
|
$result = mysql_query("SELECT * FROM bookmark");
|
|
</pre>
|
|
|
|
<pre>
|
|
//mysql_num_rows() returns the number of bookmarks found.
|
|
$rows = mysql_num_rows($result);
|
|
echo "Number of bookmarks:";
|
|
//outputs the number of records (rows)
|
|
echo $rows;
|
|
echo "<br><br>";
|
|
$i=0;
|
|
echo "<a href=sendbook.html>Insert More BookMarks</a>\n<br><br>";
|
|
echo "<table border=1>";
|
|
</pre>
|
|
|
|
<pre>
|
|
//This allows you to access the query in a form of an array.
|
|
//The array index is the name of the field of the database.
|
|
while ($row = mysql_fetch_array($result))
|
|
{
|
|
echo "<tr><td>\n";
|
|
// The . operator adds string together.
|
|
echo "<a href=".$row["URL"].">".$row["description"]."</a>\n";
|
|
echo "</td></tr>";
|
|
}
|
|
echo"</table>";
|
|
mysql_close();
|
|
echo"<a href=../index.html target=_top>";
|
|
echo "</HTML>";
|
|
?>
|
|
</pre>
|
|
|
|
With these three files you should be able to get your
|
|
first bookmark application working, but just don't stop there. Work upon
|
|
it, and make your bookmark better, and smarter. From this example, you
|
|
should be able to build several other types of online utilities, like:
|
|
guestbooks, counters, surveys, etc.
|
|
<p> I hope that this article was useful, and taught you
|
|
something new. Feel free to email me at: <a href=mailto:afsilva@liberty.edu>afsilva@liberty.edu</a>, and send me your comments
|
|
and questions.
|
|
|
|
|
|
|
|
<!-- *** BEGIN copyright *** -->
|
|
<P> <hr> <P>
|
|
<H5 ALIGN=center>
|
|
|
|
Copyright © 2000, Anderson Silva<BR>
|
|
Published in Issue 50 of <i>Linux Gazette</i>, February 2000</H5>
|
|
<!-- *** END copyright *** -->
|
|
|
|
<!--startcut ==========================================================-->
|
|
<P> <HR> <P>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<A HREF="index.html"><IMG ALT="[ Table of Contents ]"
|
|
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
|
|
<A HREF="../index.html"><IMG ALT="[ Front Page ]"
|
|
SRC="../gx/homenew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="rogers.html"><IMG ALT="[ Prev ]" SRC="../gx/back2.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="../faq/index.html"><IMG ALT="[ Linux Gazette FAQ ]"
|
|
SRC="./../gx/dennis/faq.gif"WIDTH=163 HEIGHT=60 ALIGN=bottom></A>
|
|
<A HREF="silva2.html"><IMG ALT="[ Next ]" SRC="../gx/fwd.gif" WIDTH=41 HEIGHT=60 ALIGN=bottom ></A>
|
|
<!-- *** END navbar *** -->
|
|
</BODY></HTML>
|
|
<!--endcut ============================================================-->
|