Help with 'record counter' of Database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
spear1976
Forum Newbie
Posts: 10
Joined: Sat Jul 20, 2002 1:58 pm
Location: Norway

Help with 'record counter' of Database

Post by spear1976 »

Hi,

Still newbie warning.

My jokedatabase is now getting to be almost the way i like it. But i still would like to add two new functions.
Q: I would like to add a field to the mySQL DB called E.g "counter", then would like to increase this number by one every time the record i viewed by a user.

My php page looks like:

Code: Select all

<?php

/* Connect to the DB, and retrive some simple variables, like $navbar etc. */ 

include ("funksjoner.php");
		
		/* Find the record matching the Joke requested by the user */
$sql="SELECT * FROM vitser WHERE ID = '$rec_ID'";

$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);		


if	( $num_rows == 0 ) 
	&#123;
		echo "Sorry, the record could not be found";
	&#125; 
		else 
			&#123;
				while ($row=mysql_fetch_array($mysql_result))
				&#123;
					/* Tranlsated DB fields: tittel = Title, kategori = category, gittav = sendt by (name), epost = Email, innhold= content */
					$tittel=$row&#1111;"tittel"];
					$kategori=$row&#1111;"kategori"];
					$gittav=$row&#1111;"gittav"];
					$epost=$row&#1111;"epost"];
		            $innhold = nl2br($row&#1111;"innhold"]);
					$ID=$row&#1111;"ID"];
						# SHOW RESULT
					echo "$software - $version";
					echo "<br><br>";
					echo "$navbar";
					echo "JokeNr: $ID - <b>$tittel</b><br> (<i>$gittav - $epost</I>)<br><br>";
					echo "$innhold<br><hr><br>";
				&#125;
			&#125; # END ELSE
mysql_close($connection);
	?>
My question is of course what code i would need to add if i wanted to do the following when a user vied this page:
1: See if the value is set, if not set it to 1 (if it has not been viewed before)
2: Find the current value, and increase it be one.
3: Save the new information back to the record with new values.

I'm i going about this all wrong, or is what I'm trying to do easy? Hope someone can help out, and that i have learned something when it comes to commenting and coding since the last time.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Well, assuming your counter field is called 'count', you just have to execute a simple query:

Code: Select all

UPDATE vitser SET count = count + 1 WHERE id = '$red_ID'
Just run that after you select the joke out of the database. That is what I do.
Post Reply