I am trying to update a field in a table by increasing its integer value by 1. Here is what I am using:
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?
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.
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.
# 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]
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.
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.
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.