I'm having two issues with a function that should add +1 to a database field and redirect to the target URL upon completion.
The problem is that unless I clear cache, my UPDATE query is not working. While trying to find why this happens I've come across another strange issue which doesn't update the function after I change it.
What I did was trying to comment header
and it's still redirecting me until the cache is cleared. On the other hand when header
is commented and after I clear cache my UPDATE function works every single time.
public function clickShortlink($slug) {
$connect = $this->connect();
$stmt = $connect->prepare("UPDATE `shortlink` SET clicks = clicks + 1 WHERE slug = :slug;");
$stmt->bindValue(":slug", $slug);
$stmt->execute();
$stmt = $connect->prepare("SELECT target FROM `shortlink` WHERE slug = :slug");
$stmt->bindValue(":slug", $slug);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$target = $row['target'];
}
if (isset($target)) {
$connect = null;
header("Location: ".$target);
exit;
}
$connect = null;
}
What am I missing about header
?
EDIT
This is how I call clickShortLink()
:
<?php
if (isset($_GET['gototarget'])) {
$shortlink = new Shortlink();
$url = $shortlink->clickShortlink($_GET['slug']);
}
?>
<form>
<br>
<br>
<input type="text" name="slug" value="di5gV">
<button name="gototarget">Go to target</button>
</form>