# How can I add geo coding into database

**URL:** <https://www.phphelp.com/t/how-can-i-add-geo-coding-into-database/20012>\
**Category:** General PHP Help\
**Created:** [April 8, 2013, 4:38pm UTC](https://www.phphelp.com/t/how-can-i-add-geo-coding-into-database/20012 "2013-04-08T16:38:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![genesisvh](https://www.phphelp.com/user_avatar/www.phphelp.com/genesisvh/32/891_2.png) [@genesisvh](https://www.phphelp.com/u/genesisvh)\
**Post date:** [April 8, 2013, 4:38pm UTC](https://www.phphelp.com/t/how-can-i-add-geo-coding-into-database/20012/1 "2013-04-08T16:38:25Z")

</div>

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:

[php]$address = $\_POST[“cross\_streets”];  
$prepAddr = str\_replace(’ ‘,’+’,$address);

$geocode=file\_get\_contents(‘[http://maps.google.com/maps/api/geocode/json?address=’.$prepAddr.’&sensor=false](http://maps.google.com/maps/api/geocode/json?address='.%24prepAddr.'&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

[/php]
