Hello All,
I'm working off a tutorial on building an address book with mysql and php. For a reference you can find it here:
http://php.about.com/od/finishedphp1/ss/address_book.htm
I'm having no problems with creating the database in sql, I've entered some sample data into the table with phpmyadmin, and the code below will correctly display the info on the page.
The problem I'm having is modifying the data. Whenever I click on the add contact, edit, or remove links, nothing happens. Sure, the url in the address bar will change from www.mysite.com/address.php to www.mysite.com/address.php?mode=add when I click on the add contact link, but no form appears so I can input data.
I've included my code below. Any help you could provide would be greatly appreciated.
<html>
<head>
<title>Address Book</title>
</head>
<body>
<?php
// Connect to Database
mysql_connect("mysite.com", "xxxx", "xxxx") or die(mysql_error());
mysql_select_db("address") or die(mysql_error());
if ( $mode=="add")
{
Print '<h2>Add Contact</h2>
<p>
<form action=';
echo $PHP_SELF;
Print '
method=post>
<table>
<tr><td>Last Name:</td><td><input type="text" name="last" /></td></tr>
<tr><td>First Name:</td><td><input type="text" name="first" /></td></tr>
<tr><td>Mobile Phone:</td><td><input type="text" name="mobile" /></td></tr>
<tr><td>Direct Phone:</td><td><input type="text" name="direct" /></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 (last, first, mobile, direct) VALUES ('$last', '$first', '$mobile', '$direct')");
}
if ( $mode=="edit")
{
Print '<h2>Edit Contact</h2>
<p>
<form action=';
echo $PHP_SELF;
Print '
method=post>
<table>
<tr><td>Last Name:</td><td><input type="text" value="';
Print $last;
print '" name="last" /></td></tr>
<tr><td>First Name:</td><td><input type="text" value="';
Print $first;
print '" name="first" /></td></tr>
<tr><td>Mobile Phone:</td><td><input type="text" value="';
Print $mobile;
print '" name="mobile" /></td></tr>
<tr><td>Direct Phone:</td><td><input type="text" value="';
Print $direct;
print '" name="direct" /></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 last = '$last', first= '$first', mobile = '$mobile', direct = '$direct' WHERE id = $id");
Print "Data Updated!<p>";
}
if ( $mode=="remove")
{
mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";
}
$data = mysql_query("SELECT * FROM address ORDER BY last ASC")
or die(mysql_error());
Print "<h2>Address Book</h2><p>";
Print "<table border cellpadding=3>";
Print "<tr><th width=100>Last Name</th><th width=100>First Name</th><th width=100>Mobile</th><th width=100>Direct</th><th width=100 colspan=2>Admin</th></tr>"; Print "<td colspan=6 align=right><a href=" .$_SERVER[PHP_SELF]. "?mode=add>Add Contact</a></td>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['last'] . "</td> ";
Print "<td>".$info['first'] . "</td> ";
Print "<td>".$info['mobile'] . "</td> ";
Print "<td>".$info['direct'] . "</td> ";
Print "<td><a href=" .$_SERVER[PHP_SELF]. "?id=" . $info['id'] ."&last=" . $info['last'] . "&mobile=" . $info['mobile'] ."&direct=" . $info['direct'] . "&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>