drg17404 2015-05-05 01:34
浏览 34
已采纳

发布到MySQL时如何插入GPS坐标?

What I want my code to do is get the user's IP address in the background, and when they fill out the field(s) and hit submit it sends not only the information they input, but their GPS coordinates as well to a database and table I already have set up.

Essentially what I am attempting to do is mark a user's current GPS location when they submit data to a database (like timestamping except with latitude and longitude obviously). To get the location information I followed a tutorial here Getting User Location Information with PHP that uses telize.com/geoip API to grab GPS coordinates from whatever IP Address accesses it.

I'm currently using two files to test it out: This one which houses my database connection information (with user and password omitted) as well as the class of functions needed to use the API:

<?php 
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_USER', '');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_NAME', 'geo');

$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

if(!$link) {
    die("Connection Unsuccessful");
}

class Geo 
{
    protected $api = 'http://www.telize.com/geoip/%s';

    protected $properties = [];

    public function __get($key)
    {
        if (isset($this->properties[$key])){
            return $this->properties[$key];
        } 
        return null;
    }

    public function request($ip)
     {
          $url = sprintf($this->api, $ip);
          $data = $this->sendRequest($url);

          $this->properties = json_decode($data, true);
    }

    protected function sendRequest($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);

        return curl_exec($curl);
    }
}


?>

And this one which holds the user end that executes the class, receives data and stores it in the database:

   <?php
//modeled from a tutorial found here: https://www.youtube.com/watch?v=Rr36E8NAORI 
//contains database connection details and class to get information from IP Address
require_once 'geo2.php';

//variable to detect IP address to be used with location API below and in geo2.php
//currently $_SERVER['REMOTE_ADDR'] only displays ::1 on localhost, using example IP for testing
$ipAddress = '86.174.200.225';
echo $ipAddress;

//method of pulling specific location information from API using IP Address
$geo = new Geo;

$geo->request($ipAddress);

 $lat = $geo->latitude;
 $lng = $geo->longitude;

echo $lat . $lng;

if(isset($_POST['submit'])){

    //get the manually inserted datum 'title'
    $title = mysqli_real_escape_string($link, $_POST['title']);

    // stored in a variable to TEST
    $sql = "INSERT INTO geo (id, lat, lng, title) VALUES (NULL, '$lat', '$lng', '$title')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    else { echo "Success"; }

}


?>

<form action="" method="post">
    <input name="title" type="text" />
    <input value="Submit" type="submit" />
</form> 

The core issues being that upon "submit" nothing is sent to the database and I do not receive any error messages or "Success" as intended. I am trying to find out whether my solution simply is not a solution at all or if I am making a minor mistake.

I'm running all of this on WAMPSERVER 2.5, and I know it is not a matching error as the database receives inputs when I make $lat and $lng input fields like $title.

  • 写回答

1条回答 默认 最新

  • doupinge9055 2015-05-05 02:19
    关注

    Your problem was in this line:

    if(isset($_POST['submit'])){
    

    Notice that you asked if $_POST with form name submit has value or not. The problem is, from the HTML, there are no form field with submit name:

    <form action="" method="post">
        <input name="title" type="text" />
        <input value="Submit" type="submit" />
    </form> 
    

    Fixing it as simple as adding name='submit' to your input type submit. Change the form into something like this:

    <form action="" method="post">
        <input name="title" type="text" />
        <input name="submit" value="Submit" type="submit" />
    </form> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?