Page 1 of 1

php check if entry exists

Posted: Wed Jun 17, 2009 9:35 am
by tomsace
Hey,
I have created a script so users can save their favourite games in a mysql database.
The script works by using the URL to decide which game to add to the database. For examplt http://www.website.com/add.php?game=1 each game has a unique ID which is how the game is added by using the ID in the URL like in the example. Basically what I want to do is to only allow the game to be added into the database if the game actually exists. How can I check wether the number in the URL exists with the games ID's in the database. For example if there are only 45 games in the database and a user types the URL http://www.website.com/add.php?game=46 it would show an error.

I would think that I should need something such as:

Code: Select all

<?php
$game = mysql_real_escape_string($_GET['game']);
 
if ($game = CODE NEEDED)??
echo "Game does not exist!";
else MY INSERT CODE HERE ?>
 

Re: php check if entry exists

Posted: Wed Jun 17, 2009 10:48 am
by mattpointblank
Simple. Run a query on that ID number. If the number of rows returned (use the mysql_num_rows($result) function) is 0, then it doesn't exist.

Re: php check if entry exists

Posted: Wed Jun 17, 2009 11:53 am
by tomsace
Thanks managed to get it working.

Tom!