Location: PHPKode > scripts > Php AddressBook 2 > php-addressbook-2/edit.php
<?php
/* Edit Addresses */
    # processed when form is submitted back onto itself
if ( isset($id) ) {	
    if (isset($_POST["save"])) {

	$id = $_REQUEST["id"];
	
// this line of code will allow php to convert the hidden new line elements \n with <br>
	$comment_content = str_replace("\n","\n<br>",$_POST['comments']);
	
	$firstname_escape = mysql_real_escape_string($_POST['firstname']);
	$lastname_escape = mysql_real_escape_string($_POST['lastname']);

////////////////////////////////////////////////////////////////////////////	
// This will connect to google using a hard Address and will retun Geocodes
////////////////////////////////////////////////////////////////////////////
   // Desired address
   $googleAddress = "http://maps.google.com/maps/geo?q=".urlencode($address) .'+'. urlencode($city) .'+'. urlencode($province).'+'. urlencode($country)."&output=xml&key=$key";

   // Retrieve the URL contents
   $googlePage = file_get_contents($googleAddress);

   // Parse the returned XML file
   $xml = new SimpleXMLElement($googlePage);

   // Parse the coordinate string
list($lng, $lat, $altitude) = explode(",", 
     $xml->Response->Placemark->Point->coordinates);

// Output the coordinates
//echo "Longitude: $lng, Latitude: $lat";
	
	if ($gid == '') {
		$gid = $oldgid;
	}
			
 	$sql = "UPDATE addressbook SET
		firstname='$firstname_escape',
		lastname='$lastname_escape',
		address='$address',
		suite='$suite',
		postalcode='$postalcode',
		city='$city',
		province='$province',
		country='$country',
		home='$home',
		mobile='$mobile',
		work='$work',
		work2='$work2',
		fax='$fax',
		email='$email',
		email2='$email2',
		email3='$email3',
		yahoo='$yahoo',
		gtalk='$gtalk',
		msn='$msn',
		icq='$icq',
		skype='$skype',
		website='$website',
		comments='$comment_content',
		lat='$lat',
		lng='$lng',
		gid='$gid'
  		WHERE id=$id";
		 
        # execute SQL statement
        mysql_query($sql);

        # check for errors
        if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }
        print "<form method=\"post\"><fieldset><p><b>Updated</b></p>\n<input name=\"button\" type=\"button\" id=\"fback\" tabindex=\"1\" onclick=\"parent.location='?title=fullview&id=$id'\" value=\"Back\" /><fieldset></form>\n";
    }
    else { # display edit form (not post method)

        # setup SQL statement to retrieve link that we want to edit
		$sql  = " SELECT * FROM addressbook ";
        $sql .= " WHERE id = $id ";
		
        # execute SQL statement
        $result = mysql_query($sql,$cid);

        # retrieve values
        $row = mysql_fetch_array($result);
		include("rows.php");
?>

<form name="add" method="post" action="<? echo"?title=edit&id=$id";?>" enctype="multipart/form-data">
  <fieldset>
  <h1>Personal Info:</h1>
  <hr />
  <dl>
    <div class="barLeft">
      <dt>
        <label for="firstname">First Name:</label>
      </dt>
      <dd>
        <input type="text" name="firstname" id="firstname" value='<? echo ($firstname); ?>' tabindex="1" />
      </dd>
      <dt>
        <label for="lastname">Last Name:</label>
      </dt>
      <dd>
        <input type="text" name="lastname" id="lastname" value='<? echo ($lastname); ?>' tabindex="2" />
      </dd>
      <dt>
        <label for="broupe">Groupe:</label>
      </dt>
      <dd>
        <?
////////////////////////////////////////////////////////////////////////////	
// Groupe Selection
////////////////////////////////////////////////////////////////////////////
echo "
<select name=\"gid\">
<option value='$gid'>Change a Groupe</option>";
// Connect to the groups table to get data
 # setup SQL statement
    $sql = "SELECT * FROM groups ORDER BY groups ASC";

    # execute SQL statement
    $grs = mysql_query($sql, $cid);
    if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

    # execute SQL statement
$grs = mysql_query($sql) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($grs);
			
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($grs); //get a row from our result set
	    $gid = $row["gid"];
		$groups = $row["groups"];
			
		# Output
		echo "<option value='$gid'>$groups</option>";
		
}
echo "
</select>
";
?>
      </dd>
      <dt>
        <label for="address">Address:</label>
      </dt>
      <dd>
        <input type="text" name="address" id="address" value='<? echo ($address); ?>' tabindex="3" />
      </dd>
	  <dt>
        <label for="address">suite:</label>
      </dt>
      <dd>
        <input type="text" name="suite" id="suite" value='<? echo ($suite); ?>' tabindex="3"/>
      </dd>
      <dt>
        <label for="postalcode">Postal/Zip Code:</label>
      </dt>
      <dd>
        <input type="text" name="postalcode" id="postalcode" value='<? echo ($postalcode); ?>' tabindex="3" />
      </dd>
      <dt>
        <label for="city">City:</label>
      </dt>
      <dd>
        <input type="text" name="city" id="city" value='<? echo ($city); ?>' tabindex="5" />
      </dd>
      <dt>
        <label for="province">Province/State:</label>
      </dt>
      <dd>
        <input type="text" name="province" id="province" value='<? echo ($province); ?>' tabindex="6" />
      </dd>
      <dt>
        <label for="country">Country:</label>
      </dt>
      <dd>
        <input type="text" name="country" id="country" value='<? echo ($country); ?>' tabindex="7" />
      </dd>
      <dt>
        <label for="website">Website:</label>
      </dt>
      <dd>
        <input type="text" name="website" id="website" value='<? if (!$website== "") {echo ($website);} ?>' tabindex="17" />
      </dd>
      <dt>
        <label for="website">Dont add the HTTP://</label>
      </dt>
    </div>
    <div class="barRight">
      <dt>
        <label for="comments">Comments:</label>
      </dt>
      <dt>
        <textarea name="comments" cols="36" rows="10" id="comments" tabindex="9"><? echo ($comments); ?></textarea>
      </dt>
      <dt>
        <label for="profiole">Update Profile Image:</label>
      </dt>
      <dd>
        <input type=button id="fchange" value="Change Image" onClick="javascript:popUp('update-profile.php?id=<? echo ($id); ?>')">
      </dd>
      <dt> </dt>
      <dd>
        <? if(!$profile == '') echo "<a href=\"?title=fullview&id=$id\" title=\"view\"><img src=profiles/imagethumb.php?s=$profile&w=65></a>"; ?>
      </dd>
    </div>
  </dl>
  <dl>
    <div class="clearfix"></div>
    <h1>Contact Information:</h1>
    <hr />
    <div class="barLeft">
      <dt>
        <label for="home">Home:</label>
      </dt>
      <dd>
        <input type="text" name="home" id="home" value='<? echo ($home); ?>' tabindex="8" />
      </dd>
      <dt>
        <label for="mobile">Mobile:</label>
      </dt>
      <dd>
        <input type="text" name="mobile" id="mobile" value='<? echo ($mobile); ?>' tabindex="9" />
      </dd>
      <dt>
        <label for="fax">Work:</label>
      </dt>
      <dd>
        <input type="text" name="work" id="work" value='<? echo ($work); ?>' tabindex="9" />
      </dd>
      <dt>
        <label for="fax">Work2:</label>
      </dt>
      <dd>
        <input type="text" name="work2" id="work2" value='<? echo ($work2); ?>' tabindex="9" />
      </dd>
      <dt>
        <label for="fax">Fax:</label>
      </dt>
      <dd>
        <input type="text" name="fax" id="fax" value='<? echo ($fax); ?>' tabindex="11" />
      </dd>
      <dt>
        <label for="email">E-Mail:</label>
      </dt>
      <dd>
        <input type="text" name="email" id="email" value='<? echo ($email); ?>' tabindex="12" />
      </dd>
      <dt>
        <label for="email2">E-Mail2:</label>
      </dt>
      <dd>
        <input type="text" name="email2" id="email2" value='<? echo ($email2); ?>' tabindex="13" />
      </dd>
      <dt>
        <label for="email2">E-Mail3:</label>
      </dt>
      <dd>
        <input type="text" name="email3" id="email2" value='<? echo ($email3); ?>' tabindex="14" />
      </dd>
    </div>
    <div class="barRight">
      <dt>
        <label for="yahoo">Yahoo:</label>
      </dt>
      <dd>
        <input type="text" name="yahoo" id="yahoo" value='<? echo ($yahoo); ?>' tabindex="15" />
      </dd>
      <dt>
        <label for="msn">MSN:</label>
      </dt>
      <dd>
        <input type="text" name="msn" id="msn" value='<? echo ($msn); ?>' tabindex="16" />
      </dd>
      <dt>
        <label for="icq">ICQ:</label>
      </dt>
      <dd>
        <input type="text" name="icq" id="icq" value='<? echo ($icq); ?>' tabindex="17" />
      </dd>
      <dt>
        <label for="jabber">Jabber:</label>
      </dt>
      <dd>
        <input name="jabber" type="text" id="jabber" tabindex="18" value="<? echo ($jabber); ?>" />
      </dd>
      <dt>
        <label for="gtalk">Google Talk:</label>
      </dt>
      <dd>
        <input name="gtalk" type="text" id="gtalk" tabindex="19" value="<? echo ($gtalk); ?>" />
      </dd>
      <dt>
        <label for="aim">AIM:</label>
      </dt>
      <dd>
        <input name="aim" type="text" id="aim" tabindex="20" value="<? echo ($aim); ?>" />
      </dd>
      <dt>
        <label for="skype">Skype:</label>
      </dt>
      <dd>
        <input name="skype" type="text" id="skype" tabindex="21" value="<? echo ($skype); ?>" />
      </dd>
    </div>
  </dl>
  <div class="barsubmit">
    <dl>
      <input name="oldgid" type="hidden" id="oldgid" value="<? echo ($gid); ?>" />
      <input type="submit" name="save" id="fsave" value="Save" class="button" tabindex="22" />
      <input name="button" type="button" id="fback" tabindex="23" onClick="javascript:history.back()" value="Back" />
    </dl>
  </div>
  </fieldset>
</form>
<?  } }
/* Edit Groups */

    # processed when form is submitted back onto itself
if ( isset($egid) ) {	
    if (isset($_POST["save"])) {

	$egid = $_REQUEST["egid"];
	
// this line of code will allow php to convert the hidden new line elements \n with <br>
		
 	$sql = "UPDATE groups SET
		groups='$groups'	
  		WHERE gid = $egid";
		 
        # execute SQL statement
        mysql_query($sql);

        # check for errors
        if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }
        print "<form method=\"post\"><fieldset><p><b>Updated</b></p>\n<input name=\"button\" type=\"button\" id=\"fback\" tabindex=\"1\" onclick=\"parent.location='index2.php?title=profile'\" value=\"Back\" /><fieldset></form>\n";
    }
    else { # display edit form (not post method)

        # setup SQL statement to retrieve link that we want to edit
		$sql  = " SELECT * FROM groups ";
        $sql .= " WHERE gid = $egid ";
		
        # execute SQL statement
        $result = mysql_query($sql,$cid);

        # retrieve values
        $row = mysql_fetch_array($result);
		$groups = $row["groups"];
		$gid = $row["gid"];
			
?>
<form name="add" method="post" action="<? echo"?title=edit&egid=$egid";?>" enctype="multipart/form-data">
  <fieldset>
  <h1>Groups:</h1>
  <hr />
  <dl>
    <div class="barLeft">
      <dt>
        <label for="firstname">Groupe Name:</label>
      </dt>
      <dd>
        <input type="text" name="groups" id="groups" value='<? echo ($groups); ?>' tabindex="1" />
      </dd>
      <dt>
        <label for="lastname"></label>
      </dt>
    </div>
  </dl>
  <div class="barsubmit">
    <dl>
      <input type="submit" name="save" id="fsave" value="Save" class="button" tabindex="20" />
      <input name="button" type="button" id="fback" tabindex="21" onClick="javascript:history.back()" value="Back" />
    </dl>
  </div>
  </fieldset>
</form>
<?  
// Close DB connection
} }?>
Return current item: Php AddressBook 2