Reputation: 55
I created an android application for uploads data to my webserver into PHP file and output data to .txt
file. I upgraded my server and now I support databases.
How can I convert/update the below php code toconnect with a MySQL database? In android side use HTTP POST to url mysite/location.php I want add records latitude,longitude to my database...mysql query...need code example...
<?php
$lat = $_POST["lat"]; // Declares the upload data from client as a variable
$lon = $_POST["lon"];
$textfile = "location.txt"; // Declares the name and location of the .txt file
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w ') or die("Something went wrong!"); // Opens up the .txt file for writing and replaces any previous content
$stringToWrite = " $lat\n $lon\n "; // Write location
fwrite($fh, $stringToWrite); // Writes it to the .txt file
fclose($fh);
//header("Location: index.html"); // Return to frontend (index.html)
?>
Upvotes: 0
Views: 1072
Reputation: 20437
This is quite a broad question. In brief, I would do:
In general, I would advise against "finding a tutorial" for specific design problems such as this, because the specific solution you want may not have been done before. However, the components of it have; thus, if you break the problem down, you'll find each bit much more manageable.
So, to start with, search this very site for "HTTP library POST Android", which is sure to return many code examples. Implement that, and satisfy yourself that it is working and that, broadly, you understand it. Next, try "PHP MySQL PDO insert" in a search engine, and get that working, and then repeat this process until your project is done.
Upvotes: 1
Reputation: 3022
If you want to be able to manage and maintain your code after creation, you need to learn how to do this yourself. Connecting to MySQL from PHP (at the time of this writing) is best accomplished using PDO.
EDIT:
Here is a link to example code using PDO:
https://stackoverflow.com/a/4559320/1183321
Upvotes: 1