php check if entry exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

php check if entry exists

Post 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 ?>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: php check if entry exists

Post 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.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php check if entry exists

Post by tomsace »

Thanks managed to get it working.

Tom!
Post Reply