SQL查询:增加2而不是1

时间:2022-03-12 20:44:01

I am trying to update a field in a table by increasing its integer value by 1. Here is what I am using:

我试图通过将其整数值增加1来更新表中的字段。这是我正在使用的:

function updateViews($id){

$sql = "UPDATE tweets SET tweet_views = tweet_views + 1 WHERE tweet_key = '$id'";

$result = mysql_query($sql) or die("DB Error : ". mysql_error());

return $result;

}

However, I find its incrementing by 2 each time rather than 1? What am I doing wrong?

但是,我发现每次增加2而不是1?我究竟做错了什么?

Thanks

Update

From the answers the SQL is correct. Do you think this may be affected by the rewrite engine??? I ask because I am 100% sure this doesn't run twice or that I don't make the call since there are two scripts. One that calls the function and one that holds the function! This is confusing.

从答案中SQL是正确的。你认为这可能会受到重写引擎的影响吗?我问,因为我100%确定这不会运行两次或我没有拨打电话,因为有两个脚本。一个调用函数和一个函数!这令人困惑。

Update 2

Using the debug function. I get this output:

使用调试功能。我得到这个输出:

array(4) {
  ["file"]=>
  string(35) "/home/magic/public_html/dbUpdate.php"
  ["line"]=>
  int(16)
  ["function"]=>
  string(15) "myDebugFunction"
  ["args"]=>
  array(0) {
  }
}

array(4) {
  ["file"]=>
  string(31) "/home/magic/public_html/view.php"
  ["line"]=>
  int(10)
  ["function"]=>
  string(11) "updateViews"
  ["args"]=>
  array(1) {
    [0]=>
    &string(5) "7jjdd"
  }
}

It looks as if the script is being called once but it is still getting updated twice??? HELP! :(

看起来好像脚本被调用一次,但它仍然被更新两次???救命! :(

Also from the Log file, it looks as if the scripts are being called three times??

同样来自日志文件,看起来好像是三次调用脚本?

13:16:28 id:4a6c9d7cf38016.29304000
  _SERVER[REQUEST_URI]=/lucic
  _SERVER[REDIRECT_URL]=/lucic
  /home/magic/public_html/dbUpdate.php@16 :myDebugFunction
  /home/magic/public_html/view.php@10 :updateViews
13:16:30 id:4a6c9d7eaf93e3.88114161
  _SERVER[REQUEST_URI]=/lucic
  _SERVER[REDIRECT_URL]=/lucic
  /home/magic/public_html/dbUpdate.php@16 :myDebugFunction
  /home/magic/public_html/view.php@10 :updateViews
13:16:31 id:4a6c9d7f846557.12618673
  _SERVER[REQUEST_URI]=/lucic
  _SERVER[REDIRECT_URL]=/lucic
  /home/magic/public_html/dbUpdate.php@16 :myDebugFunction
  /home/magic/public_html/view.php@10 :updateViews

UPDATE 3

Here is the contents of my htaccess file which may be causing a problem.

这是我的htaccess文件的内容,可能会导致问题。

# REWRITE DEFAULTS
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^view\.php$ /%2? [L,R=301]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]

13 个解决方案

#1


You may want to include the HTML of the resulting page here. Its possible a script, link (css) or img tag are not formatted properly, and are getting into the rewrite by just being "", "#" or "?" since those will 'resolve' to the same url. If you're not absolute-pathing all of the urls (images/header.gif instead of /images/header.gif, etc) some of those may be falling through, especially since to a browser those short urls look like directories. Try doing the request in curl (without mirroring, just a simple GET) and see if it happens there.

您可能希望在此处包含结果页面的HTML。它可能是脚本,链接(css)或img标签格式不正确,只是通过“”,“#”或“?”进入重写。因为那些将“解析”到同一个网址。如果你不是绝对路径所有的网址(images / header.gif而不是/images/header.gif等),其中一些可能会失败,特别是因为浏览器这些短网址看起来像目录。尝试在curl中执行请求(没有镜像,只需一个简单的GET),看看它是否发生在那里。

Also, turn on HTTP access and rewrite logging and see whats happening at that level (note, you have to put rewritelog directives in the compiled config, it wont work in a .htaccess). Failing that, watch the request in something like tamperdata, or even better, wireshark, to see the actual requests being made.

此外,打开HTTP访问并重写日志记录,看看在该级别发生了什么(注意,你必须在编译的配置中放置rewritelog指令,它不能在.htaccess中工作)。如果做不到这一点,请查看tamperdata或更好的wireshark等请求,以查看实际的请求。

Other things to think about: MultiViews On in your apache conf could be trying to add a suffix extension and it could be getting caught oddly by your rewrite (not sure how, but who knows) - and your rewrite log will show that. mod_dir could be trying to add a trailing slash (since those short urls look sort of like directories), though, you'd probably see that.

其他需要考虑的事项:在你的apache conf中的MultiViews On可能会尝试添加后缀扩展,并且可能会被你的重写(不确定如何,但谁知道)而被奇怪地抓住 - 并且你的重写日志会显示出来。 mod_dir可能试图添加一个尾部斜杠(因为那些短网址看起来有点像目录),但是,你可能会看到它。

#2


You are running the query more than once by mistake. :)

您错误地运行了多次查询。 :)

(ok, thats just a guess, but I'd suggest some logging to make sure)

(好吧,这只是一个猜测,但我建议一些日志记录,以确保)

#3


You can test whether your function is called twice or your script with some debug logging.

您可以测试您的函数是否被调用两次,或者您的脚本是否带有一些调试日志。

function myDebugFunction() {
  static $iid = null;

  if ( is_null($iid) ) {
    $iid = uniqid('', true);
  }

  $log = date('H:i:s').' id:'.$iid."\n".
    "  _SERVER[REQUEST_URI]=". @$_SERVER['REQUEST_URI']."\n" .
    "  _SERVER[REDIRECT_URL]=". @$_SERVER['REDIRECT_URL'];
  foreach(debug_backtrace() as $bt) {
    echo '<pre>'; var_dump($bt); echo '</pre>';
    $log .= "\n  ".$bt['file'].'@'.$bt['line'].' '.@$bt['class'].':'.$bt['function'];
  }
  $log .= "\n";
  error_log($log, 3, 'mydebug.log');
}

function updateViews($id) {
  myDebugFunction();
  $sql = "
    UPDATE
      tweets
    SET
      tweet_views = tweet_views + 1
    WHERE
      tweet_key = '".mysql_real_escape_string($id) ."'
  ";
  $result = mysql_query($sql) or die("DB Error : ". mysql_error());
  return $result;
}

If you find the same id twice in mydebug.log the function has been called twice (within the same php instance). Otherwise your script has been invoked twice.

如果在mydebug.log中找到两次相同的id,则该函数被调用两次(在同一个php实例中)。否则,您的脚本已被调用两次。

#4


There's no trigger involved, is there?

没有触发器,是吗?

#5


There are some current browsers (I think Chrome is one of them) that incorrectly issues a GET after a POST to a form, which might account for the problem you're seeing.

目前有一些浏览器(我认为Chrome就是其中之一)在POST到表单后错误地发出GET,这可能会解决您所看到的问题。

Could you log the requests to your php application as well and see if you're hit twice from the browser?

您是否可以将请求记录到您的php应用程序中,看看您是否从浏览器中点击了两次?

#6


The SQL is correct. I'd trace the PHP and make sure that there isn't a duplicate call to the function that runs the SQL.

SQL是正确的。我将跟踪PHP并确保没有对运行SQL的函数进行重复调用。

#7


Is this page an error handling page?

此页面是错误处理页面吗?

The fact that the REDIRECT_URL is the same as the REQUEST_URI suggests so since REDIRECT_URL seems to be a variable set by apache when handling ErrorDocuments or similar.

REDIRECT_URL与REQUEST_URI相同的事实表明,因为在处理ErrorDocuments或类似事件时,REDIRECT_URL似乎是由apache设置的变量。

Given that the file does indeed seem to be being run more than once some redirect looping may be occuring - what's in your .htaccess file?

鉴于该文件确实似乎不止一次运行,可能会出现一些重定向循环 - 你的.htaccess文件中有什么?

EDIT: there's way more info here: http://onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html?page=last

编辑:这里有更多信息:http://onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html?page =last

#8


just a guess, but do you have a virus scanner installed? Sometimes they prefetch a page - thus resulting in two hits. To check for that look at your access.log .

只是猜测,但你安装了病毒扫描程序吗?有时他们会预取页面 - 从而导致两次点击。要检查access.log的外观。

#9


i would create a sproc/trigger to handle this on the database side.

我会创建一个sproc /触发器来处理数据库端。

call the sproc from your page on a specific event (like a button click).

在特定事件(例如按钮单击)上从您的页面调用sproc。

your issue could be from unexpected browser refresh or it could also be from scope error on database side.

您的问题可能来自意外的浏览器刷新,也可能来自数据库端的范围错误。

redesigning your code as a stored procedure should either fix your problem or help you figure out where the issue is arising.

将代码重新设计为存储过程应该可以解决您的问题或帮助您找出问题所在。

#10


Check the encoding of the file. If the browser discovers non-ascii letters in the page, then it might reload the page with another encoding. Non-ascii letters could be æøå, Russion, Chinese or Hebrew letters. Even if you have a meta tag that specifies the encoding, the browser will still reload the page and start processing it again.

检查文件的编码。如果浏览器在页面中发现非ASCII字母,则可能会使用其他编码重新加载页面。非ascii字母可以是æøå,Russion,中文或希伯来字母。即使您有一个指定编码的元标记,浏览器仍会重新加载页面并再次开始处理。

To fix this problem open your file in an advanced browser (notepad++ for example) and specify the encoding. In Notepad++ this is in Format>Encode in UTF-8. If you use the Format>Convert to UTF-8, some strange bytes might appear at the beginning of the document, so it is safer to use the Encode method.

要解决此问题,请在高级浏览器(例如notepad ++)中打开文件并指定编码。在Notepad ++中,这是格式> UTF-8中的编码。如果使用格式>转换为UTF-8,文档的开头可能会出现一些奇怪的字节,因此使用Encode方法更安全。

#11


Do you use Smarty?

你用Smarty吗?

--edit-- I asked this because Smarty has a bug, when you have an img tag without a src defined in the template, the base index.php will be included twice.

--edit--我问这个是因为Smarty有一个bug,当你有一个没有在模板中定义src的img标签时,基础index.php将被包含两次。

The same ca happen if you have an img (you could have for ex ) or an iframe in the same page, as the base script might get displayed twice(or more) in just one refresh of the page.

如果您在同一页面中有一个img(您可能有ex)或iframe,则会发生同样的情况,因为基本脚本可能只在页面的一次刷新中显示两次(或更多)。

#12


Just randomly: does your page have any auto-refresh/reload features, to get the latest comments, prices, updates etc? That might result in a second loading of the page, which might do what you're seeing.

随机:您的页面是否有任何自动刷新/重新加载功能,以获取最新的评论,价格,更新等?这可能会导致第二次加载页面,这可能会影响您所看到的内容。

Also, do you use any AJAX?

另外,你使用任何AJAX吗?

#13


Do you use header("location: xxxxx") redirects anywhere on your page (or included pages)? If you use them without calling exit() immediately after, the script will continue to execute.

您是否在页面(或包含的页面)的任何位置使用标题(“location:xxxxx”)重定向?如果您在不立即调用exit()的情况下使用它们,脚本将继续执行。

For example, if you have a login check on an included page that sets some $_SESSION variable, then redirects you to the same page using a header() redirect, it's possible that the function exit() was not called afterward, which means the rest of the script will still get executed, including your page with the updateViews() function. However, the user won't see this because on the client side it will redirect before any data is output to the browser.

例如,如果您对包含页面设置了一些$ _SESSION变量进行了登录检查,那么使用header()重定向将您重定向到同一页面,之后可能没有调用函数exit(),这意味着脚本的其余部分仍将执行,包括具有updateViews()函数的页面。但是,用户不会看到这一点,因为在客户端,它会在任何数据输出到浏览器之前重定向。

Can we see the whole script to verify?

我们可以看到要验证的整个脚本吗?

#1


You may want to include the HTML of the resulting page here. Its possible a script, link (css) or img tag are not formatted properly, and are getting into the rewrite by just being "", "#" or "?" since those will 'resolve' to the same url. If you're not absolute-pathing all of the urls (images/header.gif instead of /images/header.gif, etc) some of those may be falling through, especially since to a browser those short urls look like directories. Try doing the request in curl (without mirroring, just a simple GET) and see if it happens there.

您可能希望在此处包含结果页面的HTML。它可能是脚本,链接(css)或img标签格式不正确,只是通过“”,“#”或“?”进入重写。因为那些将“解析”到同一个网址。如果你不是绝对路径所有的网址(images / header.gif而不是/images/header.gif等),其中一些可能会失败,特别是因为浏览器这些短网址看起来像目录。尝试在curl中执行请求(没有镜像,只需一个简单的GET),看看它是否发生在那里。

Also, turn on HTTP access and rewrite logging and see whats happening at that level (note, you have to put rewritelog directives in the compiled config, it wont work in a .htaccess). Failing that, watch the request in something like tamperdata, or even better, wireshark, to see the actual requests being made.

此外,打开HTTP访问并重写日志记录,看看在该级别发生了什么(注意,你必须在编译的配置中放置rewritelog指令,它不能在.htaccess中工作)。如果做不到这一点,请查看tamperdata或更好的wireshark等请求,以查看实际的请求。

Other things to think about: MultiViews On in your apache conf could be trying to add a suffix extension and it could be getting caught oddly by your rewrite (not sure how, but who knows) - and your rewrite log will show that. mod_dir could be trying to add a trailing slash (since those short urls look sort of like directories), though, you'd probably see that.

其他需要考虑的事项:在你的apache conf中的MultiViews On可能会尝试添加后缀扩展,并且可能会被你的重写(不确定如何,但谁知道)而被奇怪地抓住 - 并且你的重写日志会显示出来。 mod_dir可能试图添加一个尾部斜杠(因为那些短网址看起来有点像目录),但是,你可能会看到它。

#2


You are running the query more than once by mistake. :)

您错误地运行了多次查询。 :)

(ok, thats just a guess, but I'd suggest some logging to make sure)

(好吧,这只是一个猜测,但我建议一些日志记录,以确保)

#3


You can test whether your function is called twice or your script with some debug logging.

您可以测试您的函数是否被调用两次,或者您的脚本是否带有一些调试日志。

function myDebugFunction() {
  static $iid = null;

  if ( is_null($iid) ) {
    $iid = uniqid('', true);
  }

  $log = date('H:i:s').' id:'.$iid."\n".
    "  _SERVER[REQUEST_URI]=". @$_SERVER['REQUEST_URI']."\n" .
    "  _SERVER[REDIRECT_URL]=". @$_SERVER['REDIRECT_URL'];
  foreach(debug_backtrace() as $bt) {
    echo '<pre>'; var_dump($bt); echo '</pre>';
    $log .= "\n  ".$bt['file'].'@'.$bt['line'].' '.@$bt['class'].':'.$bt['function'];
  }
  $log .= "\n";
  error_log($log, 3, 'mydebug.log');
}

function updateViews($id) {
  myDebugFunction();
  $sql = "
    UPDATE
      tweets
    SET
      tweet_views = tweet_views + 1
    WHERE
      tweet_key = '".mysql_real_escape_string($id) ."'
  ";
  $result = mysql_query($sql) or die("DB Error : ". mysql_error());
  return $result;
}

If you find the same id twice in mydebug.log the function has been called twice (within the same php instance). Otherwise your script has been invoked twice.

如果在mydebug.log中找到两次相同的id,则该函数被调用两次(在同一个php实例中)。否则,您的脚本已被调用两次。

#4


There's no trigger involved, is there?

没有触发器,是吗?

#5


There are some current browsers (I think Chrome is one of them) that incorrectly issues a GET after a POST to a form, which might account for the problem you're seeing.

目前有一些浏览器(我认为Chrome就是其中之一)在POST到表单后错误地发出GET,这可能会解决您所看到的问题。

Could you log the requests to your php application as well and see if you're hit twice from the browser?

您是否可以将请求记录到您的php应用程序中,看看您是否从浏览器中点击了两次?

#6


The SQL is correct. I'd trace the PHP and make sure that there isn't a duplicate call to the function that runs the SQL.

SQL是正确的。我将跟踪PHP并确保没有对运行SQL的函数进行重复调用。

#7


Is this page an error handling page?

此页面是错误处理页面吗?

The fact that the REDIRECT_URL is the same as the REQUEST_URI suggests so since REDIRECT_URL seems to be a variable set by apache when handling ErrorDocuments or similar.

REDIRECT_URL与REQUEST_URI相同的事实表明,因为在处理ErrorDocuments或类似事件时,REDIRECT_URL似乎是由apache设置的变量。

Given that the file does indeed seem to be being run more than once some redirect looping may be occuring - what's in your .htaccess file?

鉴于该文件确实似乎不止一次运行,可能会出现一些重定向循环 - 你的.htaccess文件中有什么?

EDIT: there's way more info here: http://onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html?page=last

编辑:这里有更多信息:http://onlamp.com/pub/a/onlamp/2003/02/13/davidsklar.html?page =last

#8


just a guess, but do you have a virus scanner installed? Sometimes they prefetch a page - thus resulting in two hits. To check for that look at your access.log .

只是猜测,但你安装了病毒扫描程序吗?有时他们会预取页面 - 从而导致两次点击。要检查access.log的外观。

#9


i would create a sproc/trigger to handle this on the database side.

我会创建一个sproc /触发器来处理数据库端。

call the sproc from your page on a specific event (like a button click).

在特定事件(例如按钮单击)上从您的页面调用sproc。

your issue could be from unexpected browser refresh or it could also be from scope error on database side.

您的问题可能来自意外的浏览器刷新,也可能来自数据库端的范围错误。

redesigning your code as a stored procedure should either fix your problem or help you figure out where the issue is arising.

将代码重新设计为存储过程应该可以解决您的问题或帮助您找出问题所在。

#10


Check the encoding of the file. If the browser discovers non-ascii letters in the page, then it might reload the page with another encoding. Non-ascii letters could be æøå, Russion, Chinese or Hebrew letters. Even if you have a meta tag that specifies the encoding, the browser will still reload the page and start processing it again.

检查文件的编码。如果浏览器在页面中发现非ASCII字母,则可能会使用其他编码重新加载页面。非ascii字母可以是æøå,Russion,中文或希伯来字母。即使您有一个指定编码的元标记,浏览器仍会重新加载页面并再次开始处理。

To fix this problem open your file in an advanced browser (notepad++ for example) and specify the encoding. In Notepad++ this is in Format>Encode in UTF-8. If you use the Format>Convert to UTF-8, some strange bytes might appear at the beginning of the document, so it is safer to use the Encode method.

要解决此问题,请在高级浏览器(例如notepad ++)中打开文件并指定编码。在Notepad ++中,这是格式> UTF-8中的编码。如果使用格式>转换为UTF-8,文档的开头可能会出现一些奇怪的字节,因此使用Encode方法更安全。

#11


Do you use Smarty?

你用Smarty吗?

--edit-- I asked this because Smarty has a bug, when you have an img tag without a src defined in the template, the base index.php will be included twice.

--edit--我问这个是因为Smarty有一个bug,当你有一个没有在模板中定义src的img标签时,基础index.php将被包含两次。

The same ca happen if you have an img (you could have for ex ) or an iframe in the same page, as the base script might get displayed twice(or more) in just one refresh of the page.

如果您在同一页面中有一个img(您可能有ex)或iframe,则会发生同样的情况,因为基本脚本可能只在页面的一次刷新中显示两次(或更多)。

#12


Just randomly: does your page have any auto-refresh/reload features, to get the latest comments, prices, updates etc? That might result in a second loading of the page, which might do what you're seeing.

随机:您的页面是否有任何自动刷新/重新加载功能,以获取最新的评论,价格,更新等?这可能会导致第二次加载页面,这可能会影响您所看到的内容。

Also, do you use any AJAX?

另外,你使用任何AJAX吗?

#13


Do you use header("location: xxxxx") redirects anywhere on your page (or included pages)? If you use them without calling exit() immediately after, the script will continue to execute.

您是否在页面(或包含的页面)的任何位置使用标题(“location:xxxxx”)重定向?如果您在不立即调用exit()的情况下使用它们,脚本将继续执行。

For example, if you have a login check on an included page that sets some $_SESSION variable, then redirects you to the same page using a header() redirect, it's possible that the function exit() was not called afterward, which means the rest of the script will still get executed, including your page with the updateViews() function. However, the user won't see this because on the client side it will redirect before any data is output to the browser.

例如,如果您对包含页面设置了一些$ _SESSION变量进行了登录检查,那么使用header()重定向将您重定向到同一页面,之后可能没有调用函数exit(),这意味着脚本的其余部分仍将执行,包括具有updateViews()函数的页面。但是,用户不会看到这一点,因为在客户端,它会在任何数据输出到浏览器之前重定向。

Can we see the whole script to verify?

我们可以看到要验证的整个脚本吗?