users online hits

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

users online hits

Post by Smackie »

im not sure if this is right.. but i just want it where it will add one to the hits field in MySQL..

Code: Select all

mysql_query(&quote;INSERT INTO users (hits) VALUES('hits+1') WHERE userid = 'userid'&quote;);

Thank you
Smackie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Lose the quotes in your mathematics

Code: Select all

mysql_query(&quote;INSERT INTO `users` (`hits`) VALUES(`hits`+1) WHERE `userid` = 'userid'&quote;);
Note: It's also good practise to get into the habit of using backticks to identify mysql database elements (feild names, table names etc) in your queries ;)
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

:? well for some reason its still not adding a anything to hits in mysql :? so what am i doing wrong?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

mysql_query(&quote;INSERT INTO `users` (`hits`) VALUES(`hits`+1) WHERE `userid` = 'userid'&quote;) or die(mysql_error());
What's the error with the above? Sorry, i've had a few glasses ;)
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

im not getting any errors :? here is the sql file

Code: Select all

CREATE TABLE `users` (
  `userid` int(25) NOT NULL auto_increment,
  `user_name` varchar(25) NOT NULL default '',
  `email_address` varchar(155) NOT NULL default '',
  `website` varchar(255) NOT NULL default '',
  `msn` varchar(155) NOT NULL default '',
  `aim` varchar(55) NOT NULL default '',
  `yahoo` varchar(55) NOT NULL default '',
  `about_me` text NOT NULL,
  `password` varchar(55) NOT NULL default '',
  `show_email` varchar(55) NOT NULL default '',
  `show_msn` varchar(55) NOT NULL default '',
  `show_aim` varchar(55) NOT NULL default '',
  `show_yahoo` varchar(55) NOT NULL default '',
  `rank` varchar(55) NOT NULL default '',
  `user_level` enum('0','1','2','3','4','5') NOT NULL default '0',
  `signup_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `last_login` datetime NOT NULL default '0000-00-00 00:00:00',
  `online_time` datetime NOT NULL default '0000-00-00 00:00:00',
  `activated` enum('0','1') NOT NULL default '0',
  `hits` int(20) NOT NULL default '1',
  PRIMARY KEY  (`userid`),
  FULLTEXT KEY `Website` (`website`)
) TYPE=MyISAM AUTO_INCREMENT=32


if that helps any
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

you have to add an entire row when you INSERT something. You need to UPDATE. ;)

Code: Select all

mysql_query(&quote;UPDATE `users` SET `hits`=`hits`+1 WHERE `userid` = 'userid' LIMIT 1&quote;) or die(mysql_error());
Edit: woah. the code tag is broken. O.O

UPDATE `users` SET `hits`=`hits`+1 WHERE `userid` = 'userid' LIMIT 1
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

what you mean code take is broken?
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

when u press the button it

Code: Select all

doesnt show up
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Code: Select all

mysql_query("UPDATE users SET hits=(hits+1) WHERE userid = '$userid'");
will do it.
Post Reply