简体   繁体   English

选择并插入一个查询SQL

[英]Select and insert in one query SQL

I have a web page where a user clicks on a map, the latitute and longitude are passed to text fields, and along with some other details the data is submitted to the database. 我有一个网页,用户单击地图,纬度和经度将传递到文本字段,并且连同其他一些详细信息一起将数据提交到数据库。 I have a formula for calculating the nearest town, and I have a little sample table of towns with their latitudes and logitudes. 我有一个计算最近的城镇的公式,还有一些城镇的经纬度样本表。

At the moment I can display the nearest town and distance, but I can't get it to send to the database. 目前,我可以显示最近的城镇和距离,但无法将其发送到数据库。 I think I need to put the two queries together but I don't know how. 我想我需要将两个查询放在一起,但我不知道如何。 Please be kind, I know my code is terrible and I have lots of security issues which I'm going to try to address later. 请客气,我知道我的代码很糟糕,并且有很多安全问题,稍后我将尝试解决。 Thank you! 谢谢!

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

?>

If you need insert the result from query in a table eg: my_table could be you need an insert select query ( a single sql command composed by two part ) 如果您需要将查询的结果插入表中,例如: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"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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