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 // Connect to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("address") or die(mysql_error());
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") { Print '<h2>Add Contact</h2> <p> <form action='; echo $PHP_SELF; Print ' method=post> <table> <tr><td>Name:</td><td><input type="text" name="name" /></td></tr> <tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr> <tr><td>Email:</td><td><input type="text" name="email" /></td></tr> <tr><td colspan="2" align="center"><input type="submit" /></td></tr> <input type=hidden name=mode value=added> </table> </form> <p>'; } if ( $mode=="added") { mysql_query ("INSERT INTO address (name, phone, email) VALUES ('$name', '$phone', '$email')"); }
Next we give the users an opportunity to add data. Since we are using the same PHP page to do everything, we will make it so that different 'modes' show different options. We would place this code directly under that in our last step. This would create a form to add data, when in add mode. When submitted the form sets the script into added mode which actually writes the data to the database. Updating DataCode: if ( $mode=="edit") { Print '<h2>Edit Contact</h2> <p> <form action='; echo $PHP_SELF; Print ' method=post> <table> <tr><td>Name:</td><td><input type="text" value="'; Print $name; print '" name="name" /></td></tr> <tr><td>Phone:</td><td><input type="text" value="'; Print $phone; print '" name="phone" /></td></tr> <tr><td>Email:</td><td><input type="text" value="'; Print $email; print '" name="email" /></td></tr> <tr><td colspan="2" align="center"><input type="submit" /></td></tr> <input type=hidden name=mode value=edited> <input type=hidden name=id value='; Print $id; print '> </table> </form> <p>'; } if ( $mode=="edited") { mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email = '$email' WHERE id = $id"); Print "Data Updated!<p>"; }
The edit mode is similar to our add mode except it pre-populates the fields with the data we are updating. The main difference is that it passes the data to the edited mode, which instead of writing new data overwrites old data using the WHERE clause to make sure it only overwrites for the appropriate ID. Removing DataCode: if ( $mode=="remove") { mysql_query ("DELETE FROM address where id=$id"); Print "Entry has been removed <p>"; } 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. |