Forum Moderators: coopster

Message Too Old, No Replies

php addressbook

         

vaso

5:52 pm on Jun 11, 2011 (gmt 0)

10+ Year Member



hallo!

I am new here, not sure how it works...
actually I am a university student and I'm learning php. I have been trying for days to write some code, but after only 6 classes at uni, I feel I need some help from wherever I can get it!

If anyone can offer some advice, my problem is the following:


I want to make an address book, using php, where someone can view a table with all the listed contacts' details (e.g. id, name, phone, email) and there will be the possibility of
-adding a new contact and
-editing
-deleting an existing contact.

I have created a table with the relevant fields in my database, and I have written some code (found on the internet but it didn't work + 20hours of trying to make it work on my own.... without the proper result) which shows the table of contacts, but:

The edit doesn't work at all
When the delete works, it deletes the contacts from last to first and not the row I am selecting
when I put the code for deleting in comments, the "Add" works but
when the code for deleting is functional, then the "Add" actually "Deletes" (unless the table is empty, in which case it adds up to ONE contact).

I think it has to do with the input from my form...

So, I don't know how to solve this.
It is probably very easy for you guys, I have been reading several threads here today (most of the stuff seems too sophisticated for me to understand...)



If you can give me some hints/suggestions on what to change, please, please do!


Here is my code:

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

mysql_connect("localhost", "mydb", "mypassword") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());

if (isset($_POST['mode']))
{

$mode = $_POST['mode'];
$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'];

if ($mode=="add")

{Print '<h2>Add Contact</h2> <p>
<form action="" 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="hidden" name="mode" value="added" />
<input type="submit" value="Submit" />
</td></tr>
</table> </form> <p>'; }

if ($mode =="added")
{mysql_query ("INSERT INTO address (name, phone, email) VALUES ( '$name', '$phone', '$email')");
echo "New contact added successfully!";}

if ($mode=="edit")
{Print '<h2>Edit Contact</h2> <p>
<form action="" 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>";}


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 name ASC")
or die(mysql_error());
Print '<h2>Address Book</h2><p>
<form action="" method=post>
<table border cellpadding=3>

<tr><th width=100>Name</th><th width=100>Phone</th><th width=200>Email</th><th width=100 colspan=2>Admin</th></tr>

<td colspan=5 align=right>
<input type ="hidden" name = "mode" value="add"/> <input type = "submit" value="Add Contact"/>';


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>
<input type ="hidden" name="mode" vlaue="edit"/>
<input type ="submit" value="Edit" ?id='. $info['id'] .'&name=' . $info['name'] . '&phone=' . $info['phone'] .'&email=' . $info['email'] .'/></td>';


Print "<td>
<input type ='hidden' name='mode' value='remove'/>
<input type ='hidden' name='id' value = ".$info['id']." />
<input type ='submit' value = 'remove' /> </td></tr> ";

}

Print "</table>";
Print " </form>";

if(!$mode) echo "You may add, edit or delete a contact";
echo $mode;



?>
</body>
</html>

jspeed

4:14 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Using your logic, you need to declare your $mode variable in your form action. For add, you would have

<form action="?mode=added" method=post>

If I were you, to clean it up some, I would keep your $mode to "add/edit/delete" and inside your conditionals, execute the appropriate action. (instead of having $mode "add" and "added")

if ($mode == "add"){

if (isset($_GET['addContact'])) {
mysql_query ("INSERT INTO `address` (name, phone, email) VALUES ( '$name', '$phone', '$email')");
echo "New contact added successfully!";
}

else
Print '<h2>Add Contact</h2> <p>
<form action="?addContact&mode=add" 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="hidden" name="mode" value="added" />
<input type="submit" value="Submit" />
</td></tr>
</table> </form> <p>';

}

I'm no php guru, but that is how I handle it. Also, you need to scrub your data for malicious characters before it is inserted into your database. Look into mysql_real_escape_string: [php.net ]

vaso

4:52 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



@jspeed: thank you very much for your reply. I am just learning php now, so I really appreciate any help:)
Question: Can I use GET when the method of the form is post?

jspeed

5:07 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Yes, you can. I'm sure some here are more knowledgeable about the dos/donts regarding GET vs POST. Also, there was a type in my code, if your passing the "mode" in the URL, you wouldn't need to pass it in a hidden form field. OR vice versa.

<form action="?addContact" method=post>

and

<input type="hidden" name="mode" value="add" />

jspeed

7:42 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



To elaborate, I used if (isset($_GET['addContact'])) because setting that as your form action puts it in the URL upon submittal. You are still using $id = $_POST['id']; to get the data from the submitted form and storing it in variables, because the form method is POST. Like I said, someone else can elaborate more on this, or correct me if im wrong. Hope that helps though.

vaso

8:54 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



@jspeed: thanks, I didn't know that... I have been trying to do this for 3 days now, and I really feel that I don't know enough...