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
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.
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
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 are running the query more than once by mistake. :)
(ok, thats just a guess, but I'd suggest some logging to make sure)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With