Jump to content

How can I add geo coding into database


frank_solo

Recommended Posts

I have a script that takes the user's input and stores it in a database. Part of the script geo codes the address into lat and lng. What I would like to do is to store those results into the database. I can't seem to store the lat and lng.

 

This is part of the code from the point where it geo codes to the end of the script:

 

$address = $_POST["cross_streets"];
$prepAddr = str_replace(' ','+',$address);
 
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
 
$output= json_decode($geocode);
 
$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;


/* --------------------------------------------------------------------------------------------------
SAVE TO DATABASE ------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- */

if(!$_POST["title"] || !$_POST["rent"] || !$_POST["fees"]){
			header('location: fields.php');	
		}
		else if(!(preg_match('#^\d+(\.(\d{2}))?$#',($_POST["rent"])))){
			header('location: rent.php');
		}
//validate captcha
		else if(($_SESSION['security_code'] != $_POST['security_code']) || (empty($_SESSION['security_code'])) ){
		header( 'Location: captcha.php' ) ;
		
		}else{		
$con = mysql_connect("localhost","","");
if (!$con){die('Could not connect: ' . mysql_error());}


}
		mysql_select_db("", $con);
		$sql = "INSERT INTO apartments (username, title, county, town, type, description, cross_streets, phone, contact, office, pets, email, rooms, bath, square, rent, fees, service, space, feeornofee, lease, youtube, videotitle, lat='$lat', lng='$lng', imageurl1, imageurl2, imageurl3, imageurl4, imageurl5, imageurl6, imageurl7, imageurl8, imageurl9, imageurl10, imageurl11, imageurl12, date_created) VALUES ('".$myusername."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['county'])."', '".mysql_real_escape_string($_POST['town'])."', '".mysql_real_escape_string($_POST['type'])."', '".mysql_real_escape_string($_POST['description'])."', '".mysql_real_escape_string($_POST['cross_streets'])."', '".mysql_real_escape_string($_POST['phone'])."', '".mysql_real_escape_string($_POST['contact'])."', '".mysql_real_escape_string($_POST['office'])."', '".mysql_real_escape_string($_POST['pets'])."', '".mysql_real_escape_string($_POST['email'])."', '".$_POST[('rooms')]."', '".mysql_real_escape_string($_POST['bath'])."', '".mysql_real_escape_string($_POST['square'])."', '".mysql_real_escape_string($_POST['rent'])."', '".mysql_real_escape_string($_POST['fees'])."', '".mysql_real_escape_string($_POST['service'])."', '".mysql_real_escape_string($_POST['space'])."', '".mysql_real_escape_string($_POST['feeornofee'])."', '".mysql_real_escape_string($_POST['lease'])."', '".mysql_real_escape_string($_POST[('youtube')])."', '".mysql_real_escape_string($_POST[('videotitle')])."', '".mysql_real_escape_string($_POST['lat'])."', '".mysql_real_escape_string($_POST['lng'])."','".mysql_real_escape_string($images[1])."', '".mysql_real_escape_string($images[2])."', '".mysql_real_escape_string($images[3])."', '".mysql_real_escape_string($images[4])."', '".mysql_real_escape_string($images[5])."', '".mysql_real_escape_string($images[6])."', '".mysql_real_escape_string($images[7])."', '".mysql_real_escape_string($images[8])."', '".mysql_real_escape_string($images[9])."', '".mysql_real_escape_string($images[10])."', '".mysql_real_escape_string($images[11])."', '".mysql_real_escape_string($images[12])."', NOW())";

		$result = mysql_query($sql) or die(mysql_error());
				
		 header('Location: success.php') ;

}//end if

 

Link to comment
Share on other sites

This should resolve the issue:

 

mysql_select_db("", $con);
		$sql = "INSERT INTO apartments (username, title, county, town, type, description, cross_streets, phone, contact, office, pets, email, rooms, bath, square, rent, fees, service, space, feeornofee, lease, youtube, videotitle, imageurl1, imageurl2, imageurl3, imageurl4, imageurl5, imageurl6, imageurl7, imageurl8, imageurl9, imageurl10, imageurl11, imageurl12, date_created, lat, lng) VALUES ('".$myusername."', '".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['county'])."', '".mysql_real_escape_string($_POST['town'])."', '".mysql_real_escape_string($_POST['type'])."', '".mysql_real_escape_string($_POST['description'])."', '".mysql_real_escape_string($_POST['cross_streets'])."', '".mysql_real_escape_string($_POST['phone'])."', '".mysql_real_escape_string($_POST['contact'])."', '".mysql_real_escape_string($_POST['office'])."', '".mysql_real_escape_string($_POST['pets'])."', '".mysql_real_escape_string($_POST['email'])."', '".$_POST[('rooms')]."', '".mysql_real_escape_string($_POST['bath'])."', '".mysql_real_escape_string($_POST['square'])."', '".mysql_real_escape_string($_POST['rent'])."', '".mysql_real_escape_string($_POST['fees'])."', '".mysql_real_escape_string($_POST['service'])."', '".mysql_real_escape_string($_POST['space'])."', '".mysql_real_escape_string($_POST['feeornofee'])."', '".mysql_real_escape_string($_POST['lease'])."', '".mysql_real_escape_string($_POST[('youtube')])."', '".mysql_real_escape_string($_POST[('videotitle')])."', '".mysql_real_escape_string($_POST['lat'])."', '".mysql_real_escape_string($_POST['lng'])."','".mysql_real_escape_string($images[1])."', '".mysql_real_escape_string($images[2])."', '".mysql_real_escape_string($images[3])."', '".mysql_real_escape_string($images[4])."', '".mysql_real_escape_string($images[5])."', '".mysql_real_escape_string($images[6])."', '".mysql_real_escape_string($images[7])."', '".mysql_real_escape_string($images[8])."', '".mysql_real_escape_string($images[9])."', '".mysql_real_escape_string($images[10])."', '".mysql_real_escape_string($images[11])."', '".mysql_real_escape_string($images[12])."', NOW(), " . (float) $lat . ", " . (float) $lng . ")";

 

Btw, make sure you haven't made the field unsigned in your database and given the firld enough decimal places for the long and lat to be stored correctly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.