如何在进行SQL查询时在同一页面上使用输入变量?

时间:2017-06-27 04:59:23

标签: php html

我有一个输入字段,它接收用户地址,然后计算其经度和纬度 我有一个存储地址的数据库,其中包含各自的经度和纬度 我正在进行以下SQL查询,以将输入字段中输入的地址与数据库地址进行比较,以找到最近的位置:

 $sql4 = 'SELECT
     (ATAN(
         SQRT(
             POW(COS(RADIANS(maptable.latitude)) *
                 SIN(RADIANS(maptable.longitude) -
                     RADIANS(wishLongitude)), 2) +
             POW(COS(RADIANS(wishLatitude)) *
                 SIN(RADIANS(maptable.latitude)) -
                 SIN(RADIANS(wishLatitude)) *
                 cos(RADIANS(maptable.latitude)) *
                 cos(RADIANS(maptable.longitude) -
                     RADIANS(wishLongitude)), 2)
         ),
         SIN(RADIANS(wishLatitude)) *
         SIN(RADIANS(maptable.latitude)) +
         COS(RADIANS(wishLatitude)) *
         COS(RADIANS(maptable.latitude)) *
         COS(RADIANS(maptable.longitude) -
             RADIANS(wishLongitude))
     ) * 6371000) as distance,
     maptable.id, maptable.price, maptable.owner, maptable.myaddress, maptable.phone
 FROM maptable
 ORDER BY distance ASC
 LIMIT 15 ';
 $result = $mysqli - > query($sql4);

用户输入地址的同一页面上的输入字段:

 <form method='post' id='wishInput' >
                   <input class='form-control' id='autocomplete' placeholder='Enter your address' onFocus='geolocate()' name='myaddress' type='text'></input>
                   <input  name='wishLatitude' id='wishLatitude' >
                   <input  name='wishLongitude' id='wishLongitude'  />
                   <input type='submit' name='wishSubmit'>
                   </form>

如何在查询中使用“wishLongitude”“wishLatitude”变量

1 个答案:

答案 0 :(得分:0)

试试这个

if( $_REQUEST['wishSubmit'] )
{
    $wishLatitude = $_REQUEST['wishLatitude'];
    $wishLongitude = $_REQUEST['wishLongitude'];

    $sql4 = "SELECT
     (ATAN(
         SQRT(
             POW(COS(RADIANS(maptable.latitude)) *
                 SIN(RADIANS(maptable.longitude) -
                     RADIANS($wishLongitude)), 2) +
             POW(COS(RADIANS($wishLatitude)) *
                 SIN(RADIANS(maptable.latitude)) -
                 SIN(RADIANS($wishLatitude)) *
                 cos(RADIANS(maptable.latitude)) *
                 cos(RADIANS(maptable.longitude) -
                     RADIANS($wishLongitude)), 2)
         ),
         SIN(RADIANS($wishLatitude)) *
         SIN(RADIANS(maptable.latitude)) +
         COS(RADIANS($wishLatitude)) *
         COS(RADIANS(maptable.latitude)) *
         COS(RADIANS(maptable.longitude) -
             RADIANS($wishLongitude))
     ) * 6371000) as distance,
     maptable.id, maptable.price, maptable.owner, maptable.myaddress, maptable.phone
     FROM maptable
     ORDER BY distance ASC
     LIMIT 15 ";
     $result = $mysqli - > query($sql4);
}

您必须使用"附上查询,以便可以在查询中使用php变量。