I have a snippet that inserts data in a row in a MySql database. If that data already exists the existing row should be updated. In my case, I continue to get insert only. What is wrong with this snippet?
$page = basename($_SERVER['SCRIPT_NAME']);
$ip_address = $_SERVER['REMOTE_ADDR'];
include_once("includes/connection.php");
$sql=mysqli_query($con, "SELECT visits FROM visitors WHERE page = $page AND ip_address =$ip_address");
if (mysqli_num_rows($sql) > 0)
{
$sqlUpdate="UPDATE visitors SET visits = visits + 1 WHERE ip_address = '".$ip_address."' AND page ='".$page."' ";
mysqli_query($con,$sqlUpdate);
}
else
{
$sqlInsert="INSERT INTO visitors (visits, page, ip_address) VALUES ('1', '".$page."', '".$ip_address."')";
mysqli_query($con,$sqlInsert);
}