Page 1 of 1

users online hits

Posted: Fri Jun 17, 2005 9:10 pm
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

Posted: Fri Jun 17, 2005 9:14 pm
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 ;)

Posted: Fri Jun 17, 2005 9:21 pm
by Smackie
:? well for some reason its still not adding a anything to hits in mysql :? so what am i doing wrong?

Posted: Fri Jun 17, 2005 9:24 pm
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 ;)

Posted: Fri Jun 17, 2005 9:27 pm
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

Posted: Fri Jun 17, 2005 9:43 pm
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

Posted: Fri Jun 17, 2005 10:03 pm
by Smackie
what you mean code take is broken?

Posted: Fri Jun 17, 2005 10:34 pm
by method_man
when u press the button it

Code: Select all

doesnt show up

Posted: Sat Jun 18, 2005 12:12 am
by Jim_Bo
Hi,

Code: Select all

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