简体   繁体   中英

How can I connect android uploads PHP data with MySQL database?

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)
?>

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

This is quite a broad question. In brief, I would do:

  • You'll need an HTTP library on your Java/Android side to connect to your PHP server, and to retrieve a string response.
  • Then your PHP code will need to save the data to a database via MySQL/PDO or MySQLi .
  • Add some security to your PHP side, so that only your Android application can save data. Will you use a username and password per user, or a GUID generated at install time? Decide what level of security you need, and trade it off with user convenience.
  • The server side will need to specify a response, probably using JSON (see json_encode in the manual)
  • The JSON string will need to be decoded in the Java side.
  • You will have to deal with internet connection problems, either reporting an error to the user, or scheduling a retry at another time.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM