nitrous
nitrous

Reputation: 1767

What is wrong with my view counter

I am trying to make a view counter that counts every time someone views the page.

my code:

$old_views = $thread_view_count;
$new_views = $thread_view_count + 1;
$sql3 = mysql_query("UPDATE forum_posts SET view_count='.$new_views.' WHERE id='.$thread_id.'");

The counter doesn't update the views of the page. I am not getting any errors, it just doesn't work.

Please help me, I am new to PHP so don't give me a hard time and destroy my self confidence

Thanks

Upvotes: 0

Views: 69

Answers (2)

user557846
user557846

Reputation:

easier

$sql3 = mysql_query("UPDATE forum_posts SET view_count=view_count+1 WHERE id='.$thread_id.'");

Upvotes: 0

Jasper
Jasper

Reputation: 76003

You are mixing your single and double quotes. You start with double quotes but then insert the variables using single quotes, which will be evaluated as a string in the query.

$sql3 = mysql_query('UPDATE forum_posts SET view_count='.(integer)$new_views.' WHERE id='. (integer)$thread_id);

Also notice I cast your values as integers so you know an integer is being passed into the database.

Upvotes: 4

Related Questions