Every time I get a visit to my index page I need to check if a record exists already in my database, if it does, it will need to update the field 'count' if not it will add a row.
I've managed to get it to create a row on visit, but cant seem to get it to update the count if the row already exists.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Check to see if record exists
$sql = "SELECT * FROM page_tracking where name ="index.php"";
if (mysqli_query ($conn, $sql)) {
$sql= "UPDATE page_tracking SET count=count+1 where name= "index.php")";
}
else if {
//Insert Query
$sql = "INSERT INTO page_tracking (name, count)
VALUES ('index.php', '1')";
}
if (mysqli_query($conn, $sql)) {
echo "Thanks for visiting! Your visit has been recorded";
} else {
echo "Unfortunately we were unable to record your visit: " . $sql . "<br>" . mysqli_error($conn);
}
There's probably a really simple explanation! Issue is definitely with the code for 'check to see if record exists'