2

This is regarding my other question: Saving Latitude and Longitude values to a MySQL Database iOS

Can you tell me if this code is saving my latitude and longitude values as strings or numbers. If it's saving as strings can you tell me how to save them as numbers?

Also I've set up the mysql fields for them as float types.

PHP

<?php

$username = "username";
$database = "database";
$password = "password";

mysql_connect(localhost, $username, $password);

@mysql_select_db($database) or die("unable to find database");

$name = $_GET["name"];
$address = $_GET["address"];
$city = $_GET["city"];
$county = $_GET["county"];
$country = $_GET["country"];
$postcode = $_GET["postcode"];
$date = $_GET["date"];
$time = $_GET["time"];
$latitude = $_GET["latitude"];
$longitude = $_GET["longitude"];

$query = "INSERT INTO map VALUES ('', '$name', '$address', '$city', '$county', '$country', '$postcode', '$date', '$time', '$latitude', '$longitude')";

mysql_query($query) or die(mysql_error("error"));
mysql_close();

?>

AddSessionViewController.h

#import <UIKit/UIKit.h>

#define kpostURL @"http://myurl.com/map.php"
#define kname @"name"
#define kaddress @"address"
#define kcity @"city"
#define kcounty @"county"
#define kcountry @"country"
#define kpostcode @"postcode"
#define kdate @"date"
#define ktime @"time"
#define klatitude @"latitude"
#define klongitude @"longitude"

@interface addSessionViewController : UIViewController {

IBOutlet UITextField *nameText;
IBOutlet UITextField *addressText;
IBOutlet UITextField *cityText;
IBOutlet UITextField *countyText;
IBOutlet UITextField *countryText;
IBOutlet UITextField *postcodeText;
IBOutlet UITextField *dateText;
IBOutlet UITextField *timeText;
IBOutlet UITextField *latitudeText;
IBOutlet UITextField *longitudeText;
NSURLConnection *postConnection;

}

-(IBAction)post:(id)sender;
-(IBAction)getLatLon:(id)sender;


-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country  withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *) latitude withLongitude:(NSString *) longitude;

AddSessionViewController.m

-(void) postMessage:(NSString*) postcode withName:(NSString *) name withAddress:(NSString *) address withCity:(NSString *) city withCounty:(NSString *) county withCountry:(NSString *) country withDate:(NSString *) date withTime:(NSString *) time withLatitude:(NSString *)latitude withLongitude:(NSString *)longitude; {

if (name != nil && address != nil && city != nil && county != nil && country != nil && postcode !=nil && date != nil && time != nil && latitude != nil && longitude != nil){

    NSMutableString *postString = [NSMutableString stringWithString:kpostURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@", kname, name]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kaddress, address]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcity, city]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcounty, county]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcountry, country]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kpostcode, postcode]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", kdate, date]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", ktime, time]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klatitude, latitude]];

    [postString appendString:[NSString stringWithFormat:@"&%@=%@", klongitude, longitude]];

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}

}


-(IBAction)post:(id)sender{

[self postMessage:(NSString*) postcodeText.text withName:nameText.text withAddress:addressText.text withCity:cityText.text withCounty:countyText.text withCountry:countryText.text withDate:dateText.text withTime:timeText.text withLatitude:latitudeText.text withLongitude:longitudeText.text];

[postcodeText resignFirstResponder];
nameText.text = nil;
addressText.text = nil;
cityText.text = nil;
countyText.text = nil;
countryText.text = nil;
postcodeText.text = nil;
dateText.text = nil;
timeText.text = nil;
latitudeText.text = nil;
longitudeText.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Test2" object:self];
}


-(IBAction)getLatLon:(id)sender{


NSString *eventName = [nameText text];
NSString *eventAddress = [addressText text];
NSString *eventCity = [cityText text];
NSString *eventCountry = [countryText text];
NSString *eventCounty = [countyText text];
NSString *eventDate = [dateText text];
NSString *eventPostCode = [postcodeText text];
NSString *eventTime = [timeText text];

// Use this to build the full event string
NSString *addressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@,%@", eventName, eventAddress, eventCity, eventCounty, eventCountry, eventPostCode, eventDate, eventTime];

// A search can't inlude the event and time and hour as it means nothing to MapKit
// The geocodeAddressString simply contains the address info
NSString *geocodeAddressString = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", eventAddress, eventCity, eventCounty, eventCountry, eventPostCode];

//NSLog(@"Hello %@",addressString);
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder geocodeAddressString:geocodeAddressString completionHandler:^(NSArray* placemarks, NSError* error){
    // Check for returned placemarks
    if (placemarks && placemarks.count > 0) {
        CLPlacemark *topResult = [placemarks objectAtIndex:0];

        //NSLog([topResult locality]);
        //NSLog([topResult location]);
        //MKCoordinateRegion region;
        //double *value = topResult.region.center.latitude;

        CLLocationDegrees latitude = topResult.region.center.latitude;
        CLLocationDegrees longitude = topResult.location.coordinate.longitude;

        NSString *latitudeString = [NSString stringWithFormat:@"%f",latitude];
        NSString *longitudeString = [NSString stringWithFormat:@"%f",longitude];

        latitudeText.text = latitudeString;
        longitudeText.text = longitudeString;

        NSLog(@"Lat: %f", latitude);
        NSLog(@"Long: %f", longitude);
    }
}];
}
Community
  • 1
  • 1
krisscross90
  • 47
  • 11

2 Answers2

1

What happens when a city name has an apostrophe in it? You might want to look into mysql_real_escape_string.

Anyway, they will be stored as floats. Data is always stored as the datatype of the column (well, unless you count null as a datatype :p).

The quotes around latitude and longitude in your query are actually unnecessary if they're floats (though you would want to make sure that they were actually floats before the removing the quotes or you'd be opening up an SQL injection hole).

What will happen now is that when the query is executed, MySQL will silently cast the strings to float values.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • Thank you for your quick reply Corbin! I'm currently trying to adapt a map kit tutorial to show addresses that I have stored in the database as annotations on my map view. I can't get it working and so thought maybe this was the reason why, but I guess it's something in my other code :) I'll look into mysql_real_escape_string too! – krisscross90 May 22 '12 at 00:59
  • No problem. And you definitely should (and SQL injection too, which it prevents). It's really a shame how many PHP/MySQL tutorials exist that completely neglect proper escaping of strings in SQL x.x. – Corbin May 22 '12 at 01:03
0

They will save in the format of the DB field. So if you have "int" they will be converted to whole numbers, if you have float they will be rounded to the nearest float, if you have it as VarChar it will be saved as a string.

To save as numbers, convert the field type in the table structure to a number - assuming you've validated the entry as a number then the same SQL query will suffice (but could be improved, before others point that out :) )

Robbie
  • 17,605
  • 4
  • 35
  • 72