I followed the steps at this website to create a background service using Android Studio that sends GPS data to a PHP script, that then posts to a MySQL database.
However, I am wanting to post to a Postgres database. I tried several things, but I have not had any success. These are the two PHP scripts from the website tutorial:
config.php script:
<?php
class DB{
private $dbHost="localhost";
private $dbUsername="USER";
private $dbPassword="PASS";
private $dbName="locationdb";
public $db_con;
public function __construct(){
if(!isset($this->db)){
try{
$pdo=new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName, $this->dbUsername, $this->dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db_con=$pdo;
}catch(PDOEXCEPTION $e){
die("Failed to connect with mysql :".$e->getMessage());
}
}
}
public function addGPSData($lat, $lon)
{
$sql="INSERT into tblgpslocation(latval, lonval) VALUES (:lat, :lon)";
$stmt=$this->db_con->prepare($sql);
$stmt->bindParam(":lat", $lat);
$stmt->bindParam(":lon", $lon);
$stmt->execute();
$newId=$this->db_con->lastInsertId();
return $newId;
}
}
?>
Script 2:
<?php
include "config.php";
$db= new DB();
if(isset($_POST['gpsdata']) && $_POST['gpsdata']=='true'){
$result=$db->addGPSData($_POST['latitude'], $_POST['longitude']);
if($result>0){
echo "OK";
}else{
echo "error";
}
}
?>
What changes do I need to make in order to make this connect to my postgresql database and post the incoming data?