Reputation: 31
So, I'm currently designing a website as kind of a demo/project. I'm doing computer sciences in college and this includes some web design. I'm looking to go in to web design in the future, and just felt like designing a website with a bunch of different functions just to scratch up on my knowledge and have something to show potential employers.
I'm currently learning PHP, so my issue is probably being caused by something glaringly obvious... I just can't see it.
$sql = "SELECT ID, name, description, views FROM videos ORDER BY ID DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$viewIncrement = "UPDATE videos SET views = views + 1 WHERE ID=" . $row["ID"] . " LIMIT 1";
if ($conn->query($viewIncrement) === TRUE) { echo " Success."; } else { echo " Failure: " . $conn->error . "."; }
}
}
So, here's the basic code.
It's part of a larger system, where I echo some HTML to display the video that is selected (the most recent addition), and then add 1 to the 'views' row. It works fine, except it increments 'views' by two, rather than one.
So, what am I doing wrong here?
Thanks in advance.
Upvotes: 2
Views: 86
Reputation: 359
Based on the code you posted, it looks like the query is getting executed twice.
One thing to check -- do you by any chance have a broken tag on the page you're loading to run this script?
This:
<img src="/bad/image/path" />
Will actually cause some browsers to load the page a second time. I've personally experienced this.
I don't know if other broken resources (such as bad or css imports) will also cause this.
Upvotes: 2