topical media & game development

talk show tell print

mashup-delicious-06-example6-5.php / php



  <?php
  //connect to the database
  link = mysql_connect('localhost','contacts_user','password')
      or die(mysql_error());
  @mysql_select_db('example_contacts') or die(mysql_error());
  
  //handle incoming form data
  if (isset(_POST['action']))
  {
      if (_POST['action'] == 'new_contact')
      {
          insert_contact_query = sprintf("insert into contacts 
              (name,company) values ('\%s','\%s')",
              mysql_escape_string(_POST['name']),
              mysql_escape_string(_POST['company'])
              );
          mysql_query(insert_contact_query);
      }
      if (_POST['action'] == 'edit_contact')
      {
          //delete or save changes?
          if (isset(_POST['delete'])) {
              edit_contact_query = sprintf("delete from contacts where id=\%s",
                  _POST['id']);
          } else {
              edit_contact_query = sprintf("update contacts 
                  set name='\%s', company='\%s' where id = \%s",
                  mysql_real_escape_string(_POST['name']),
                  mysql_real_escape_string(_POST['company']),
                  _POST['id']
              );
          }
          //execute the query
          mysql_query(edit_contact_query);
      }
  
  }
  
  //fetch all contacts
  all_contacts_query = "select * from contacts";
  all_contacts_result = mysql_query(all_contacts_query);
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <title>Example Contacts Database</title>
  </head>
  <body>
  <h2>Contact List</h2>
  <table border="1" cellspacing="2" cellpadding="2">
      <tr>
          <th>id</th>
          <th>name</th>
          <th>company</th>
      </tr>
      <?php
      while (row = mysql_fetch_assoc(all_contacts_result)) {
          ?>
          <tr>
          <form method="post">
              <td><?php echo row['id'] ?></td>
              <td>
                  <input type="text" name="name" 
                      value="<?php echo row['name'] ?>" />
              </td>
              <td>
                  <input type="text" name="company"
                      value="<?php echo row['company'] ?>" />
              </td>
              <td>
                  <input type="hidden" name="id" value="<?php echo row['id'] ?>" />
                  <input type="hidden" name="action" value="edit_contact" />
                  <input type="checkbox" name="delete" /> Delete
                  <input type="submit" value="Save Changes" />
              </td>
          </form>
          </tr>
          <?php
      }
      ?>
  </table>
  <h2>Add a New Contact</h2>
  <form method="post">
      Name: <input type="text" name="name" />
      Company: <input type="text" name="company" />
      <input type="hidden" name="action" value="new_contact" />
      <input type="submit" value="Save" />
  </form>
  </body>
  </html>
  <?php
  //close the database connection
  mysql_close(link);
  ?>


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.