检查选择查询的结果然后比较当前值(如果可用)然后不应插入

时间:2017-02-13 11:59:54

标签: php mysql

我在地理定位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";


 }

1 个答案:

答案 0 :(得分:0)

据了解,您需要检查天气是否为database Table只有在发现错误时才会插入。

我正在使用面向PHP对象的mysqli预处理语句。

只有当latitute和longitute相同时,此代码才返回false。 如果你想要输出返回false,那么任何人都匹配输出,而不仅仅是在OR查询中添加SELECT运算符。

这是包含数据的表格图像

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