Simple Address Book

This tutorial will walk you through creating a simple address book using PHP and MySQL.

Before you can begin you need to decide what fields you 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 include more options if you like.

01
of 06

The Database

To create this database you 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", "sunshine@fakeaddress.com"), ( "Devie", "658-555-5985", "potato@monkey.us" )

This creates our database fields and puts in a couple of temporary entries for you to work with. You are creating four fields. The first is a ​self incrementing number, then name, phone and email. You will use the number as a unique ID for each entry when editing or deleting.

02
of 06

Connect to the Database

 <html>
<head>
<title>Address Book</title>
</head>
<body>

<?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("address") or die(mysql_error());

Before you can do anything, you need to connect to the database. We have also included an HTML title for the address book. Be sure to replace your host address, username, and password with the appropriate values for your server.

03
of 06

Add a Contact

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'll give the users an opportunity to ​add data. Since you are using the same PHP page to do everything, you will make it so that different 'modes' show different options. You 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.

04
of 06

Updating Data

 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 the add mode except it pre-populates the fields with the data you 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.

05
of 06

Removing Data

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.

06
of 06

The Address Book

 $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 the script actually pulls the data from the 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.