php - 在一个查询 SQL 中选择并插入

标签 php mysql

我有一个网页,用户在 map 上单击,纬度和经度将传递到文本字段,并且数据与其他一些详细信息一起提交到数据库。我有一个计算最近城镇的公式,并且有一个带有纬度和经度的城镇样本表。

目前我可以显示最近的城镇和距离,但无法将其发送到数据库。我想我需要将这两个查询放在一起,但我不知道如何。请友善,我知道我的代码很糟糕,而且我有很多安全问题,稍后我将尝试解决这些问题。谢谢!

<?php

 $conn    = Connect();

/**
 * Use the Haversine Formula to display the 100 closest matches to $origLat, $origLon
 * Only search the MySQL table $tableName for matches within a 10 mile ($dist) radius.
 */

$origLat = $conn->real_escape_string($_POST['lat']);
$origLon = $conn->real_escape_string($_POST['lng']);

// This is the maximum distance (in miles) away from $origLat, $origLon in which to search
$dist = 50; 

$query = "SELECT Townland, lat, lng, 3956 * 2 * 
          ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2)
          +COS($origLat*pi()/180 )*COS(lat*pi()/180)
          *POWER(SIN(($origLon-lng)*pi()/180/2),2))) 
          as distance FROM townland WHERE 
          lng between ($origLon-$dist/cos(radians($origLat))*69) 
          and ($origLon+$dist/cos(radians($origLat))*69) 
          and lat between ($origLat-($dist/69)) 
          and ($origLat+($dist/69)) 
          having distance < $dist ORDER BY distance limit 1"; 

$result = mysqli_query($conn, $query) or die(mysql_error());
while($row = mysqli_fetch_assoc($result)) {
    echo $row['Townland']." > ".$row['distance']."<BR>";
}


$User_ID = $conn->real_escape_string($_POST['User_ID']);
$Species_Name = $conn->real_escape_string($_POST['Species_Name']);
$No_Of_Birds = $conn->real_escape_string($_POST['No_Of_Birds']);
$newdate = $conn->real_escape_string($_POST['Sighting_Date']);
//Converts date to 'yyyy-mm-dd' acceptable to mysql
$newdate=date('Y-m-d',strtotime($newdate));    
$Sighting_Details = $conn->real_escape_string($_POST['Sighting_Details']);
$lat = $conn->real_escape_string($_POST['lat']);
$lng = $conn->real_escape_string($_POST['lng']);
$townland = $row['Townland'];

$query = "INSERT into sighting 
                (User_ID, Species_Name, No_Of_Birds, 
                 Sighting_Date, Sighting_Details, 
                 lat, lng, Townland) 
           VALUES('" . $User_ID . "','" . $Species_Name . "','" . $No_Of_Birds . "','"
             . $newdate . "','" . $Sighting_Details . "','" 
             . $lat . "','" . $lng . "','" . $townland . "')";

$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);
} 

header("Location: ../View/thankYouSubmitSighting.php");

$conn->close();
exit();

while($row = mysqli_fetch_assoc($result)) {
    echo $row['Townland']." > ".$row['distance']."<BR>";
}
mysqli_close($conn); 

?>

最佳答案

如果您需要将查询结果插入表中,例如:my_table 可能您需要一个插入选择查询(由两部分组成的单个sql命令)

 "INSERT  into my_table (Townland, lat, lng, distance)
 SELECT Townland, lat, lng, 3956 * 2 * 
        ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2)
        +COS($origLat*pi()/180 )*COS(lat*pi()/180)
        *POWER(SIN(($origLon-lng)*pi()/180/2),2))) 
        as distance FROM townland WHERE 
        lng between ($origLon-$dist/cos(radians($origLat))*69) 
        and ($origLon+$dist/cos(radians($origLat))*69) 
        and lat between ($origLat-($dist/69)) 
        and ($origLat+($dist/69)) 
        having distance < $dist ORDER BY distance limit 1"; 

关于php - 在一个查询 SQL 中选择并插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898349/

相关文章:

php - laravel 无法根据价格对记录进行排序

mysql - 如何对所有标题进行求和,即使其他标题为空

php - docker php :7. 1-fpm-alpine 无法构建 GD

php - 在我的帐户中添加个人资料图片(文件上传)> 在 WooCommerce 中编辑帐户

php - 如何调试变量未发布到 MYSQL 数据库

mysql - 什么时候用sql,什么时候用存储过程?

php - 无法找到驱动程序 - lamp debian

php - 尝试上传文件时 $_FILES 为空

php - 我怎样才能使这个 mysql 查询工作而不会得到空结果?

mysql - 禁用 ONLY_FULL_GROUP_BY