|
Posted by Mr. Newt on 01/01/07 11:29
Hi y'all,
I'm new to PHP and found a script on about.com that is supposed to make an
address book. Doesn't work (for me) though. First thing I noticed is that
a variable ($mode) is not declared. When I do declare it, the script still
doesn't work properly.
Note: The database and table exists with 2 rows, which shows up on the
address book page, so I'm reasonably sure it's not a MySQL issue.
Please give me some clues.
Thanks.
Robert
***********
<html>
<head>
<title>Address Book</title>
</head>
<body>
<?php
/*I've tried manually declaring $mode here with empty "" as well as
function_names ex:
$mode="remove"; */
// Connects to your Database [br]
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//add mode adds a form
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>';
}
//added mode adds the data
if ( $mode=="added")
{
mysql_query ("INSERT INTO address (name, phone, email) VALUES ('$name',
'$phone', '$email')");
}
//edit mode adds a pre-populated form
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>';
}
//edited mode updates the data for a given ID
if ( $mode=="edited")
{
mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email =
'$email' WHERE id = $id");
Print "Data Updated!<p>";
}
//Remove mode removes data for a given ID
if ( $mode=="remove")
{
mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";
}
//This pulls the data and puts it into an array, then prints in alphabetical
order based on name
$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>
[Back to original message]
|