Hello, all:

I have a simple chart, like...
ID ACCOUNT YEAR JAN FEB
1 Utilities 2007 $20 $20
2 Utilities 2008 $25 $25

Let's say, customer wants to add a "year" like 2009, how can I check that this "2009" is NOT already in the YEAR column?? so that if it's NOT, then go ahead and insert new year, and if it was, then DO NOT insert. and instead echo something like "year already exists!"

I am kind of a newbie, and I've tried several ways, but cant seem to figure it out!

Aprpeciate the help! thanks...

$userinput = $_POST[example];
$result = mysql_query("SELECT * FROM example WHERE year='$userinput'")
or die(mysql_error());
$total = mysql_num_rows( $result );
if ($total == 0){
echo "The year " .$userinput ." already exists!";
}
else
{

mysql_query("INSERT INTO example
(year) VALUES('$userinput') ")
or die(mysql_error());
echo "The year: " .$userinput ." has been added successfully";
}

Hmmm, so you look for records with the user's selected year in it, thats where I would have started. But if we don't find any such rows, then it must already exist? I think not. Try $total > 0 .

Thanks guys...

I see now how I should have simply matched the POST entry vs. the DB!! I seemed to have been doing it all in-reverse, like first SELECTING the records, and then comparing the POST against each row with a "while" or with "in_array", which would then repeat the message repeatedly... what a mess I was doing! I definitely need to be more creative and logical with php... such a simple solution and almost had a brain-meltdown!

Aprpeciate the help!

This question has already been answered. Start a new discussion instead.