$db = mysql_connect("localhost", "euicho_power", "k1nn1s0n") //open a connection to the db
or die("Unable to connect to mysql");
mysql_select_db("euicho_powermap",$db) //select the powermap database
or die("Could not select" + $dbname);
$first = TRUE;
if($townname != "" && ($latitude != "" || $longitude != "" || $zipcode !="") ) {
//if town name isn't blank and at at least 1 other field is not blank
$townname = mysql_real_escape_string($townname);
$latitude = mysql_real_escape_string($latitude);
$longitude = mysql_real_escape_string($longitude);
$zipcode = mysql_real_escape_string($zipcode);
$result = mysql_query("SELECT lat, lng, zip FROM Towns WHERE name LIKE '$townname'"); //look for town in db
if(mysql_numrows($result) > 0) { //the town is in the db already,
$oldlat = mysql_result($result, 0, 'lat');
$oldlng = mysql_result($result, 0, 'lng');
$oldzip = mysql_result($result, 0, 'zip');
$query = "UPDATE Towns SET";
if($latitude != "") { $query = $query . " lat='$latitude'"; $first = FALSE; }
if($longitude != "") {
if($first) { $query = $query . " lng='$longitude'"; $first = FALSE; }
else { $query = $query . ", lng='$longitude'"; }
}
if($zipcode != "") {
if($first) { $query = $query . " zip='$zipcode'"; }
else { $query = $query . ", zip='$zipcode'"; }
}
$query = $query . " WHERE name='$townname'";
$result = mysql_query($query); //run the update query
$result = mysql_query("SELECT name, lat, lng, zip FROM Towns WHERE name LIKE '$townname'"); //look for town in db
if(mysql_numrows($result) > 0) { //town was found
$name = mysql_result($result, 0, 'name');
$lat = mysql_result($result, 0, 'lat');
$lng = mysql_result($result, 0, 'lng');
$zip = mysql_result($result, 0, 'zip');
echo("Town Updated
\n");
echo("The town of '" . $name . "' was successfully updated.
");
echo("Old Latitude: " . $oldlat . "
");
echo("New Latitude: " . $lat . "
");
echo("Old Longitude: " . $oldlng . "
");
echo("New Longitude: " . $lng . "
");
echo("Old Zip Code: " . $oldzip . "
");
echo("New Zip Code: " . $zip . "
");
echo("Return to Power Map.
\n");
}
else { echo("An error seems to have occured after making changes. Please contact the administrator.
"); }
}
else { //the town is new, add it to the db:
if($townname != "" && $latitude != "" && $longitude != "") { //all key fields are present:
$query = "INSERT INTO Towns VALUES (NULL,'$townname','$latitude','$longitude'";
if($zipcode != "") { $query = $query . ",'$zipcode',NULL)"; }
else { $query = $query . ",NULL,NULL)"; }
$result = mysql_query($query); //run the insert query
//get id
$result = mysql_query("SELECT id FROM Towns WHERE name='$townname'");
$newid = mysql_result($result, 0, 'id');
//Report back to the user:
echo("Town Added
\n");
echo("The town of '" . $townname . "' was successfully added to the database:
\n");
echo("Latitude: " . $latitude . "
");
echo("Longitude: " . $longitude . "
");
echo("Zip Code: " . $zipcode . "
");
echo("Return to Power Map.
\n");
}
else { //missing a key field:
echo("Error Adding Town
\n");
echo("Error: only the zip code may be omitted for new towns.
\n");
echo("Please go back and try again.
\n");
}
}
}
else {
echo("Input Error
\n");
echo("Error: the town name and at least 1 field cannot be blank.
\n");
echo("Please go back and try again.
\n");
}
mysql_close($db); //close the connection to the db
?>