Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Incrementing a database value not working!
Old 11-17-2009, 10:48 AM Incrementing a database value not working!
Novice Talker

Posts: 14
Name: Alex Stubbs
Trades: 0
I am using a simple script to count clicks of a link... problem is that when it is supposed to increment the value of a table in the DB... it, well, doesn't.

Here is the click.php:
HTML Code:
    ...
        // get id and protect it
        $id = htmlspecialchars($_GET[id]);      
        // check db
        $get = mysql_fetch_assoc(mysql_query("SELECT * FROM `affiliates` WHERE `id` = '$id' LIMIT 1"));        
        // increment hits
        $insert = mysql_query("UPDATE `affiliates` SET `out` = 'out+1' WHERE `id` = '$id'");  

    break;
}
?>
It doesn't increment 'out' at all... and if i set the value of "out" to something other than 0 and call it, it defaults it back to 0.

What am i doing wrong?
alexjames01 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-17-2009, 11:57 AM Re: Incrementing a database value not working!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Again, in sql, the result of your operation depends of the field datatype.

If your column out is a varchar, you cannot use the db to make out+1, because 'something'+1='something1' (or an exception)

You can do that if your column is a numeric type, and in that case, you have to drop the single quotes, which denotes a varchar
Code:
UPDATE affiliates SET out = out+1
--and not
UPDATE affiliates SET out = 'out+1'
Now, if your field is in a text datatype, the best would be to convert it to integer.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-17-2009 at 12:01 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-17-2009, 12:23 PM Re: Incrementing a database value not working!
Novice Talker

Posts: 14
Name: Alex Stubbs
Trades: 0
It is an int type... and with or without the single quotes I'm still having no luck. Running a query without the single quotes gives me a syntax error... i tried it many times before as that was how all of the examples i found were on other sites. With the quotes it will (if the column is not 0) increase it by 1... if i run it again, it goes back to default (0).

Maybe I'm missing something though. The database was set up like:

Code:
CREATE TABLE `affiliates` (
    `id` int(11) NOT NULL auto_increment,
    `name` varchar(255) NOT NULL default '',
    `banner` text NOT NULL,
    `url` text NOT NULL,
    `email` varchar(255) NOT NULL default '',
    `in` int(11) NOT NULL default '0',
    `out` int(11) NOT NULL default '0',
    `active` int(1) NOT NULL default '0',
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM;
I'm definitely somewhat new to most of this so i am a little lost here.
alexjames01 is offline
Reply With Quote
View Public Profile
 
Old 11-17-2009, 12:40 PM Re: Incrementing a database value not working!
Novice Talker

Posts: 14
Name: Alex Stubbs
Trades: 0
Well i got it working by using:

$insert = mysql_query("UPDATE affiliates SET `out` = `out`+'1' WHERE id = '$id'");

(the single quotes on 'out' before + '1')
alexjames01 is offline
Reply With Quote
View Public Profile
 
Old 11-17-2009, 12:44 PM Re: Incrementing a database value not working!
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I dont know for what, but could it be that 'out' is a reserved SQL key word? That would explain why you had to escape it with the `` quote things (what ever they're called ). Otherwise Tripy's code snippet should work fine.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-17-2009, 02:58 PM Re: Incrementing a database value not working!
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, out is not a reserved word.

I forgot to talk about it, but if the previous value of the column was null, then it's perfectly clear that out+1 stays null.
Null can be interpreted as "unknown", and "unknown"+1 is still unknown...
But as the definition says "not null", I must say that I'm puzzled...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-17-2009 at 03:32 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-19-2009, 09:05 AM Re: Incrementing a database value not working!
Super Talker

Posts: 139
Name: John Davis
Trades: 0
Quote:
Originally Posted by alexjames01 View Post
It is an int type... and with or without the single quotes I'm still having no luck. Running a query without the single quotes gives me a syntax error... i tried it many times before as that was how all of the examples i found were on other sites. With the quotes it will (if the column is not 0) increase it by 1... if i run it again, it goes back to default (0).
tripy was right. You have to use UPDATE `affiliates` SET `out` = out+1. It's works, I've checked.

Please, provide us vervion of your MySQL and error messages that you've received.
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Reply     « Reply to Incrementing a database value not working!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22393 seconds with 12 queries