I've been doing some research on mysqli_real_escape_string() but I'm not really understanding how to properly use it in my case to help protect against SQLInjection, Using my code below, can someone help me correct this? I appreciate all the help. The other questions on here reguarding sql injection and php did not really answer my question reguarding the proper syntax usage in my format, When I used this:
"$city = mysqli_real_escape_string($_POST['City']);
I got just my generic search no matter what the input of '%$city%' or '%$business%'
<?php
$con = mysqli_connect(........);
// Check connection
if (mysqli_connect_errno())
{
echo "<option>Failed to connect to the Database</option>" ;
}
$city = mysqli_real_escape_string($con, $_POST['City']);
$business = mysqli_real_escape_string($con, $_POST['Business']);
$result = mysqli_query($con,"SELECT * FROM Business WHERE City LIKE '%$city%' AND BName LIKE '%$business%' ORDER BY City, BName ASC");
while($row = mysqli_fetch_array($result))
{
// do stuff here
}
// No other results
echo "<center>No other listings like $city or $business</center>";
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
mysql_real_escape_string()and the legacy mysql extension—it's abandoned and insecure. If you are using the new mysqli extension, use it all the time (among other reasons, because you can't mix two different database extensions). Prepared statements are the way to go; period.