Reputation: 107
I have made an tracking app in android which successfully generates latitude and longitude
but I need to store lat and long in my external mySQL database using PHP.
I am using value type "decimal(10,7)" for lat and long in DB.
Any ideas how to store lat and long generated from android into my external mySQL database using PHP?
My PHP CODE
<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];
$sender=$_POST['sender'];
$hostname = 'localhost';
$username = 'xxxx';
$password = 'xxxxxxx';
$conn = mysql_connect($hostname,$username,$password) or die('Error Please Try Again.');
if ($conn) {
mysql_select_db("android track", $conn);
$query2 = "INSERT INTO coor VALUES(NULL,'$sender','$lat','$lng')";
if (mysql_query($query2, $conn))
echo "Data Insertion Successful";
else
echo "Oops". mysql_error() ;
}
mysql_close();
?>
Upvotes: 0
Views: 2619
Reputation: 34657
Ok, few things before I rewrite your code...
auto increment
and specify your columns explicitly in
SQL.SHOW
CREATE TABLE coordinates
into your question. I suspect you're passing a value of null
in a column that's a primary key
and can never be null.Upvotes: 1
Reputation: 3494
You can submit the mLat
and mLong
variables to the server using the code below:
String storeURL = "http://www.yourdomain.com/yourscript.php?lat=" + mLat + "&long=" + mLong;
URL getURL;
getURL = new URL(storeURL);
URLConnection connection = getURL.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(getURL.openStream());
OutputStream output = new ByteArrayOutputStream();
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
String downloadResult = output.toString();
Log.i("LOG", downloadResult); //Your php-script can, for example, "echo("OK");" if the storing was successful
You still need to take care of error-handling (no network connection, etc.), of course.
Upvotes: 1