简体   繁体   English

更新查询无法按预期工作

[英]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. 但是每次重新加载页面时,db中的计数都会增加2。 What is going on here? 这里发生了什么? Thanks.. 谢谢..

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. 您的页面包含imgscript没有或没有src attr。
  • Request to favicon lead to your page. 请求favicon导致您的页面。
  • Your browser has extension which send extra requests. 您的浏览器具有发送额外请求的扩展名。
  • Your css contains something like this: {background-image: url();} 你的css包含这样的内容: {background-image: url();}

Explore your browser console for requests. 浏览浏览器控制台以获取请求。

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'

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. 在你在查询中调用变量之前尝试声明一个变量,如下所示:我也不确定你的'count'是字符串还是变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM