Ian Pither
Ian Pither

Reputation: 53

$variable not working in SELECT

Why would the following SELECT statement be returning the else statement? and a blank echo of $latitude and $longitude

$latitude = $_GET["lat"];
$longitude = $_GET["long"];
$pScan="";
$sql = "SELECT id, agent_name, address, url, latitude, longitude FROM enabled_boards WHERE latitude='$latitude' AND longitude='$longitude' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $id=$row["id"];
        $agent=$row["agent_name"];
        $address=$row["address"];
        $url=$row["url"];
        $latitude=$row["latitude"];
        $longitude=$row["longitude"];
        $pScan.='  <div id="wrapper">
        <a href="propertyDisp.php?id=' . $id . '" target="_self" >
  <div id="agentIdent"><img src="http://arpad.property/logos/' . $agent . '.jpg"></div>
  <div id="propertyIdent"><h3 align="right">' . $address . '</h3></div>
    </div></a>'; 

    }
}else{
$pScan="No results found,<br>please ensure the property is Arpad Enabled.";
}

Whereas replacing the SELECT statement values of $latitude and $longitude with the actual values from the form (lat & long) it successfully echo's both $latitude and $longitude correctly, proving that the values in the form (lat & long) are parsing correctly via the 2 variables.

Using $latitude & $longitude in the SELECT statement breaks it and I can't see why.

Thanks for your help. Ian.

FORM DETAILS

 <form action="scanBoard.php" method="post" name="scanBoard" >
      <input type="text" id="lat" value="52.562292" /><br>
       <input type="text" id="long" value="-0.660871" /><br>
     <div id="boardBtn" class="blink"><input class="button" type="image" src="img/boardBtn.png" ></div></form>

*The values have been added to the inputs for testing and will be removed once I fix this issue.

USING $latitude AND $longitude in SELECT statement.

incorrect

USING 52.562292 AND -0.660871 in SELECT statement.

enter image description here

Upvotes: 1

Views: 528

Answers (2)

Saty
Saty

Reputation: 22532

Always create you query to prevent sql injection .Column name and table name is in backtick.

 $sql = "SELECT `id`, `agent_name`, `address`, `url`, `latitude`, `longitude` FROM `enabled_boards` WHERE `latitude`='".$latitude."' AND `longitude`='".$longitude."' ";

And always use mysqli_connect_errno to check the error in your query

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

Updated

You forget form name in your input field

<input type="text" id="lat" value="52.562292" />
<input type="text" id="long" value="-0.660871" />

It would be

 <input type="text" id="lat" value="52.562292" name="lat" />
 <input type="text" id="long" value="-0.660871" name="long" />

And instead of GET method

$latitude = $_GET["lat"];
$longitude = $_GET["long"];

Use POST method because you use method="post" so it would be

$latitude = $_POST["lat"];
$longitude = $_POST["long"];

Upvotes: 1

Sanath Ballal
Sanath Ballal

Reputation: 1738

Your query has to be

$sql = "SELECT id, agent_name, address, url, latitude, longitude FROM enabled_boards WHERE latitude='" . $latitude. "' AND longitude='". $longitude ."'";

Upvotes: 1

Related Questions