title: addressbook in php
body: <p>hallo!</p>
  
  <p>I am a university student, and I want to make an address book, using php, where someone can view a table with all the listed contacts' details (e.g. name, phone, email) and there will be the possibility of</p>
  
  <p>-adding a new contact and</p>
  
  <p>-editing
  -deleting an existing contact.</p>
  
  <p>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:</p>
  
  <p>The edit doesn't work at all</p>
  
  <p>When the delete works, it deletes the contacts from last to first and not the row I am selecting</p>
  
  <p>when I put the code for deleting in comments, the "Add" works</p>
  
  <p>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).</p>
  
  <p>If anyone can give me some hints/suggestions on what to change, please please do!</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Address Book&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt; 
  &lt;?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 '&lt;h2&gt;Add Contact&lt;/h2&gt; &lt;p&gt; 
  &lt;form action="" method=post&gt; 
  &lt;table&gt; 
  &lt;tr&gt;&lt;td&gt;Name:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="name" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;Phone:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="phone" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;Email:&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="email" /&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan="2" align="center"&gt;
  &lt;input type="hidden" name="mode" value="added" /&gt;
  &lt;input type="submit" value="Submit" /&gt;
  &lt;/td&gt;&lt;/tr&gt; 
  &lt;/table&gt; &lt;/form&gt; &lt;p&gt;'; }
  
  if ($mode =="added") 
  {mysql_query ("INSERT INTO address (name, phone, email) VALUES ( '$name', '$phone', '$email')"); 
  echo "New contact added successfully!";}
  
  if ($mode=="edit") 
  {Print '&lt;h2&gt;Edit Contact&lt;/h2&gt;  &lt;p&gt; 
  &lt;form action="" method=post&gt; 
  &lt;table&gt; 
  &lt;tr&gt;&lt;td&gt;Name:&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="'; 
  Print $name;  print '" name="name" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;Phone:&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="'; 
  Print $phone; print '" name="phone" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;tr&gt;&lt;td&gt;Email:&lt;/td&gt;&lt;td&gt;&lt;input type="text" value="'; 
  Print $email; print '" name="email" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  
  &lt;tr&gt;&lt;td colspan="2" align="center"&gt;&lt;input type="submit" /&gt;&lt;/td&gt;&lt;/tr&gt; 
  &lt;input type=hidden name=mode value=edited&gt; 
  &lt;input type=hidden name=id value='; Print $id; print '&gt; 
  &lt;/table&gt; 
  &lt;/form&gt; &lt;p&gt;'; 
  } 
  
  if($mode=="edited") 
  {mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email = '$email' WHERE id = $id"); 
  Print "Data Updated!&lt;p&gt;";}
  
  
  if ($mode=="remove") 
  {mysql_query ("DELETE FROM address where id=$id");
  Print "Entry has been removed &lt;p&gt;";}
  
  }
  
  
  
  
  $data = mysql_query("SELECT * FROM address ORDER BY name ASC") 
   or die(mysql_error()); 
  Print '&lt;h2&gt;Address Book&lt;/h2&gt;&lt;p&gt;
  &lt;form action="" method=post&gt; 
  &lt;table border cellpadding=3&gt;
  
  &lt;tr&gt;&lt;th width=100&gt;Name&lt;/th&gt;&lt;th width=100&gt;Phone&lt;/th&gt;&lt;th width=200&gt;Email&lt;/th&gt;&lt;th width=100       colspan=2&gt;Admin&lt;/th&gt;&lt;/tr&gt;
  
  &lt;td colspan=5 align=right&gt;
  &lt;input type ="hidden" name = "mode" value="add"/&gt; &lt;input type = "submit" value="Add Contact"/&gt;';
  
  
  while($info = mysql_fetch_array( $data )) 
  { 
  Print "&lt;tr&gt;&lt;td&gt;".$info['name'] . "&lt;/td&gt; "; 
  Print "&lt;td&gt;".$info['phone'] . "&lt;/td&gt; "; 
  Print "&lt;td&gt; &lt;a href=mailto:".$info['email'] . "&gt;" .$info['email'] . "&lt;/a&gt;&lt;/td&gt;"; 
  
  Print '&lt;td&gt;
  &lt;input type ="hidden" name="mode" vlaue="edit"/&gt;
  &lt;input type ="submit" value="Edit"  
  ?id='. $info['id'] .'&amp;name=' . $info['name'] .'
      &amp;phone=' . $info ['phone'] .'&amp;email=' . $info['email'] .'/&gt;&lt;/td&gt;'; 
  
  
  Print "&lt;td&gt;
  &lt;input type ='hidden' name='mode' value='remove'/&gt;
  &lt;input type ='hidden' name='id' value = ".$info['id']." /&gt;
  &lt;input type ='submit' value = 'remove' /&gt; &lt;/td&gt;&lt;/tr&gt;  "; 
  
  } 
  
  Print "&lt;/table&gt;";
  Print " &lt;/form&gt;"; 
  
   if(!$mode) echo "You may add, edit or delete a contact";
  echo $mode;
  
  
  
   ?&gt; 
  &lt;/body&gt; 
   &lt;/html&gt;
  </code></pre>