user1285659
user1285659

Reputation: 123

update query not working as expected

i have this simple update query :

update `table_name` set `count` = `count` + 1 where `id` = '$id'

But every time i reload the page the count is incremented by 2 in the db. What is going on here? Thanks..

Upvotes: 1

Views: 92

Answers (3)

Kevin G Flynn
Kevin G Flynn

Reputation: 231

Try Below Code : Fetch count first from that table and increment it and then update the table

$count = SELECT count(*) FROM `table_name`
$count1 = $count + 1;
update `table_name` set `count` = '$count1' where `id` = '$id'

Upvotes: 1

Algie Rosario
Algie Rosario

Reputation: 1

Try declaring a variable before you call it in the query like this : Also im not sure if your 'count' is a string or also a variable.

$count2 = 'count' + 1;
update `table_name` set `count` = '$count2' where `id` = '$id'

Upvotes: 0

sectus
sectus

Reputation: 15454

It's a browser issue. Your browser visit page twice.

Why it could be:

  • Your page contains img or script without or empty src attr.
  • Request to favicon lead to your page.
  • Your browser has extension which send extra requests.
  • Your css contains something like this: {background-image: url();}

Explore your browser console for requests.

Upvotes: 2

Related Questions