Who's Online
We have 8 guests onlineBreadcrumbs
| Address Book |
The Database
![]()
This tutorial will walk you through creating a simple address book using PHP and MySQL. Before we can begin we need to decide what fields we wish to include in our address book. For this demonstration we will use Name, E-mail and Phone number, although you can modify it to included more options if you like.
To create this database we need to execute this code: CREATE TABLE address (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), phone VARCHAR(30), email VARCHAR(30)); INSERT INTO address (name, phone, email) VALUES ( "Alexa", "430-555-2252", " This e-mail address is being protected from spambots. You need JavaScript enabled to view it "), ( "Devie", "658-555-5985", " This e-mail address is being protected from spambots. You need JavaScript enabled to view it " ) This creates our database fields, and puts in a couple of temporary entries for us to work with. We are creating four fields. The first is a self incrementing number, then name, phone and email. We will use the number as a unique ID for each entry when editing or deleting.
Connect to the DatabaseCode: <html><head> <title>Address Book</title> </head> <body> <?php
Before we can do anything, we need to connect to the database. I have also included an HTML title for our address book. Be sure to replace your host address, username, and password with the appropriate values for your server.
Add A ContactCode: if ( $mode=="add")
Updating DataCode:
if ( $mode=="edit")
Removing DataCode: if ( $mode=="remove")
To remove data we simply query the database to remove all the data related to the entries ID.
The Address BookCode: $data = mysql_query("SELECT * FROM address ORDER BY name ASC")or die(mysql_error()); Print "<h2>Address Book</h2><p>"; Print "<table border cellpadding=3>"; Print "<tr><th width=100>Name</th><th width=100>Phone</th><th width=200>Email</th><th width=100 colspan=2>Admin</th></tr>"; Print "<td colspan=5 align=right><a href="/ .$_SERVER[’PHP_SELF’]. "?mode=add>Add Contact</a></td>"; while($info = mysql_fetch_array( $data )) { Print "<tr><td>".$info['name'] . "</td> "; Print "<td>".$info['phone'] . "</td> "; Print "<td> <a href=mailto:".$info['email'] . ">" .$info['email'] . "</a></td>"; Print "<td><a href="/ .$_SERVER[’PHP_SELF’]. "?id=" . $info['id'] ."&name=" . $info['name'] . "&phone=" . $info['phone'] ."&email=" . $info['email'] . "&mode=edit>Edit</a></td>"; Print "<td><a href="/ .$_SERVER[’PHP_SELF’]. "?id=" . $info['id'] ."&mode=remove>Remove</a></td></tr>"; } Print "</table>"; ?> </body> </html> The bottom part of our script actually pulls the data from our database, puts it into an array, and prints it out. Using the PHP_SELF function with actual database data, we are able to link to add mode, edit mode, and remove mode. We pass the appropriate variables within each link, to let the script know which mode is needed. From here you can make aesthetic changes to this script, or try adding more fields.
|
