如何使用html表单将php地理编码地址插入mysql数据库

问题描述 投票:-6回答:1

我如何将php地理编码地址插入mysql数据库我有像id,地址,纬度,经度等文件我也不想设置数据硬编码我想通过html表单发送它但表单应该只有1个textinput只有地址,它应该存储在数据库中的不同字段中,例如:在输入字段中输入地址,它应该插入到数据库地址字段中,并且纬度和经度也应该在我搜索它的同时存储2天但没有找到任何结果任何帮助将不胜感激。

php mysql google-maps geocoding
1个回答
0
投票

虽然这个问题非常雄心勃勃,但你会得到一些答案......我会列出一些资源来帮助你开始......

有关如何开始处理https://www.w3schools.com/php/php_forms.asp的表格,请参阅W3Schools

请参考MySQLi开始创建连接并对MySQL数据库执行“insert”语句https://www.php.net/manual/en/book.mysqli.php

编辑:显然链接不是一个正确的答案...所以这是一个[正确的] [编辑2:但不准确的问题]答案...

<?php 
if(isset($_POST) && count($_POST) > 0){
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
$stmt = mysqli_prepare( $link, "INSERT INTO Location(Address, Latitude, Longitude) VALUES(?, ?, ?)");\
$stmt->bind_param("sdd", $_POST['Address'], $_POST['Latitude'], $_POST['Longitude']);
$stmt->execute();
}
?>
<html>
<body>
<form action='#' method='POST'>
<input name='Address' type='text' />
<input name='Latitude' type='text' />
<input name='Longitude' type='text' />
</form>
</body>
</html>

编辑2:这是Google的地理编码(您需要有一个Google Api Key,可在此处找到:https://cloud.google.com/maps-platform/?apis=maps

  <?php
  if(isset($_POST) && count($_POST) > 0){
  //Initate Curl
  $ch = curl_init();
  //GeoCoding Address
  $Google_API_KEY = "*****************";
  $url = "https://maps.googleapis.com/maps/api/geocode/json?key={$Google_API_KEY}&address=" . urlencode($_POST['Address']) . "&sensor=false";
  //Handle the Curl Params
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  //Execute Curl and Decode JSON
  $response = json_decode(curl_exec($ch), true);
  //var_dump($response);
  $geometry = $response['results'][0]['geometry'];
  $longitude = $geometry['location']['lat'];
  $latitude = $geometry['location']['lng'];
  $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
  $stmt = mysqli_prepare( $link, "INSERT INTO Location(Address, Latitude, Longitude) VALUES(?, ?, ?)");
  $stmt->bind_param("sdd", $_POST['Address'], $latitude, $longitude );
  $stmt->execute();
  }
  ?>
  <html>
  <body>
  <form action='tester.php' method='POST'>
  <input name='Address' type='text' />
  </form>
  </body>
  </html>

请原谅简化

编辑3:我不确定为什么这“太宽泛”。但是,我会说这个问题没有尝试过他们尝试过的东西......但是如果他们确实尝试了两天,那么让我们解读一下用户说的话......

(1)如何将php地理编码地址插入mysql数据库

  • 在我的代码中,我展示了如何将地理编码的地址插入到mysql数据库中

(2)我有id,地址,纬度,经度等字段

  • 只需注明哪些领域是必要的

(3)我也不想设置硬编码的数据我希望通过html表单发送它但表单应该只有1个textinput仅用于地址

  • 关于流应该是什么以及在html中输入1个文本的要求的说明

(4)它应该存储在数据库的不同字段中,例如:在输入字段中输入地址,它应该插入到数据库地址字段以及纬度和经度

  • 在我的代码中,它确实查找了地址,然后在数据库中输入纬度和经度

(5)也应该在我搜索的同时存储

  • 在我的代码中它确实如此

(6)超过2天但没有找到任何结果任何帮助将不胜感激。

  • 请求帮助。别担心,我给你<3

因为这被认为是上网编码帮助的最佳地点,我想死,为什么不帮助[新]用户。无论如何,我相信我确实回答了用户问的[歧义]问题

© www.soinside.com 2019 - 2024. All rights reserved.