1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
<!--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="lg_toc50.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../lg_frontpage.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="../lg_faq.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="lg_toc50.html"><IMG ALT="[ Table of Contents ]"
SRC="../gx/indexnew.gif" WIDTH=163 HEIGHT=60 ALIGN=bottom ></A>
<A HREF="../lg_frontpage.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="../lg_faq.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 ============================================================-->
|