2017-02-13 101 views
1

我將地理位置API的幫助保存latlong到MySQL數據庫但問題是一樣的latlong插入數據庫。我試圖檢查最後一行我的MySQL表,然後與當前的latlong比較,如果兩者都是相同的,它不應該執行。請幫助我得到這個..提前感謝。檢查結果從選擇查詢然後與當前值比較,如果可用,那麼不應該插入

$latitude = 19.1579; 
$longitude = 72.9935; 
$address = airoli; 


$sql = "SELECT latitude FROM tracklatlong ORDER BY id DESC LIMIT 1"; 
$result = mysqli_query($sql, $conn); 
$row = mysqli_fetch_array($result); 

$currentlat = $_row["latitude"]; 


if($currentlat != $latitude){ 

$query = "INSERT INTO `tracklatlong` (latitude, longitude,address) VALUES ('$latitude','$longitude','$address')"; 

    if($conn->query($query) === TRUE){ 
     echo "success"; 
    } 
    else{ 
     echo "failed"; 
    } 


} 

else{ 


    echo"Already exists"; 


} 
+0

所以你想檢查第一拉特長在分貝,然後想要添加 –

+0

''currentlat = $ _row [「latitude」];'=>'$ currentlat = $ row [「latitude」];' – JustBaron

+0

在複合體'(緯度,經度)'上添加'UNIQUE'約束。 – eggyal

回答

0

如所理解的,你需要檢查渡過latitute或longitute是database Table只有當發現錯誤插入。

我正在使用面向mysqli準備語句的PHP對象。

此代碼僅當同時出現和結束時才返回false。 如果您希望輸出返回false,只要在SELECT查詢中添加OR運算符即可。

這裏是與數據的表圖像

tracklatlong table

這裏是HTML代碼:的index.php

<?php 
    include('co_ordinate.php'); 
    $newcoordinate = new co_ordinate(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 

<!-- jQuery library --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

<!-- Latest compiled JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <div class="col-md-6 col-md-offset-3" style="margin-top:100px;"> 
     <form action="" method="post"> 
      <div class="form-group"> 
       <i>Add Latitute</i> 
       <input type="text" name="latitute" class="form-control"> 
      </div> 

      <div class="form-group"> 
       <i>Add Longitute</i> 
       <input type="text" name="longitute" class="form-control"> 
      </div> 

      <div class="form-group"> 
       <input type="submit" name="addcoordinate" class="btn btn-primary"> 
      </div> 
     </form> 
     <?php 
      if(isset($_POST['addcoordinate'])){ 
       $latitude = $_POST['latitute']; 
       $longitute = $_POST['longitute']; 

       $newcoordinate->getCo_ordinates($latitude,$longitute); 
      } 
     ?> 
    </div> 
</body> 
</html> 

下面是類文件:co_ordinate.php

<?php 
    class co_ordinate{ 
     private $link; 

     function __construct(){ 
      $this->link = new mysqli ('localhost','root','','example'); 
      if(mysqli_connect_errno()){ 
       die("connection Failed".mysqli_connect_errno()); 
      } 
     } 

     function getCo_ordinates($latitude,$longitute){ 
      $sql = $this->link->stmt_init(); 
      if($sql->prepare("SELECT latitude,longitude FROM tracklatlong WHERE latitude=? AND longitude= ?")){ 
       $sql->bind_param('dd',$latitude,$longitute); 
       $sql->execute(); 
       $sql->store_result(); 
       if($sql->num_rows > 0){ 
        echo "The Co-Ordinates Already Exists"; 
       } 
       else 
       { 
        $query = $this->link->stmt_init(); 
        if($query->prepare("INSERT INTO tracklatlong (latitude,longitude) VALUES (?,?)")){ 
         $query->bind_param('dd',$latitude,$longitute); 
         $query->execute(); 
         echo "The Co-Ordinates Inserted Successfully"; 
        } 
       } 
      } 
      else 
      { 
       echo $this->link->error; 
      } 
     } 
    } 
?>