<?php
$emailName  = $_GET['emailName'];
$headline    = $_GET['headline'];
$headerText = $_GET['headerText'];
if ($eid) {
  $q = 'UPDATE `emails` SET ' .
             '`emailName`="' . $emailName . '", ' . 
             '`headline`="' . $headline . '", ' .
             '`headerText`="' .  $headerText . '"' . 
             ' WHERE `id`="' . $eid . '";';
}
else {
  $q = "INSERT INTO `emails` (`location_id`, 
                                    `emailName`, 
                                    `headline`, 
                                    `headerText` 
                                   )
        VALUES ('$locID', '$emailName', '$headline', '$headerText');";
}
if ($res = mysql_query($q, $link)) {
  if (!($eid)) { // get the email_id that was created with the new insert
    $q = 'SELECT `id` ' .
               'FROM `emails` ' .
               'WHERE ' .
               '`emailName`="' . $emailName . '" AND ' .                
               '`headline`="' . $headline . '" AND ' . 
               '`headerText`="' . $headerText .
                '";';
    if ($res = mysql_query($q, $link)) {
      $row =  mysql_fetch_assoc($res);
      $eid = $row['id'];
    }
  }
  $qs   .= "location=$locID&email_id=$eid";
}        