Jump to content

Archived

This topic is now archived and is closed to further replies.

moret

Increment one in MySQL

Recommended Posts

Hi all,

I'm doing a forum, and I have a table field named 'hits'. I want to increment one the value of this field each time a user visits a topic. It seems easy, but when page executes:

[code]$result=mysql_query("UPDATE forum_messages SET hits=hits+1 WHERE id='$id");[/code]

MySQL increments 2 instead 1, why?

Thanks.

Share this post


Link to post
Share on other sites
for pure semantics change your query to:

[code]<?php

$result=mysql_query("UPDATE `forum_messages` SET `hits` = `hits` + 1 WHERE `id` ='$id'");

?>[/code](also note you were missing a closing apostrophe after $id)

now check that you don't run the query twice in the process of your application logic.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.