[ Table of Contents ] [ Front Page ] [ Prev ] [ Linux Gazette FAQ ] [ Next ]

"Linux Gazette...making Linux just a little more fun!"


Developing Web Applications - Part III

By Anderson Silva



At this time, I will close the Developing Web Applications 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 creating your own online bookmark. Once you understand this example, you will be able to do basic mySQL operations with php3.

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:

Choose whatever fits you better. For this small project, I will give you the configuration that fits the needs for this application.

    Database Host: myserver
    Username: myusername
   Password: mypassword
   Database Name:  mydatabase
   Table Name: bookmark
    Fields in the bookmark table:  id, url, description

All the information above is relevant when coding the application. Note: The fields are the columns on the database. The id was defined to allow every entry in your database to be unique (primary key), it should be defined to be unique, and auto-increment.

Once you have your database defined and working, you may start coding your application, and here is how it goes:
 

The HTML form that will capture the data and send it in to the database: [text copy of this listing]

    <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>
The above form will have to text fields: one for the URL, the other for the URL description. The form tag will be responsable for telling the browser what to do when the Submit button is pressed. In this case it will call the php3 script sendbook.php3, and send the data to that script.

The following script is the sendbook.php3. This script will open a connection to the mySQL database, and send the data from the HTML form to the database. [text copy]

         <?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>";
            }
         ?>
The third and last script is called book.php3. This script will query the data entered by sendbook.php3, and display on the screen all of your bookmarks. [text copy]
    <?     echo "<HTML>";
           echo "<HEAD><TITLE>Afsilva's Bookmark</title></head>";
           echo "<body bgcolor=white>";
           echo "<IMG SRC=bookmark.jpg><br><br>";
                //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");
                //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>";
                //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>";
      ?>
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.

I hope that this article was useful, and taught you something new. Feel free to email me at: afsilva@liberty.edu, and send me your comments and questions.


Copyright © 2000, Anderson Silva
Published in Issue 50 of Linux Gazette, February 2000


[ Table of Contents ] [ Front Page ] [ Prev ] [ Linux Gazette FAQ ] [ Next ]