how can I check if a value already in a column, and if so, either NOT insert new valu

Thread Solved

Join Date: Mar 2007
Posts: 63
Reputation: websurfer is an unknown quantity at this point 
Solved Threads: 0
websurfer websurfer is offline Offline
Junior Poster in Training

how can I check if a value already in a column, and if so, either NOT insert new valu

 
0
  #1
Feb 8th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Re: how can I check if a value already in a column, and if so, either NOT insert new valu

 
0
  #2
Feb 8th, 2009
$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";
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 363
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: how can I check if a value already in a column, and if so, either NOT insert new valu

 
0
  #3
Feb 8th, 2009
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 .
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 63
Reputation: websurfer is an unknown quantity at this point 
Solved Threads: 0
websurfer websurfer is offline Offline
Junior Poster in Training

Re: how can I check if a value already in a column, and if so, either NOT insert new valu

 
0
  #4
Feb 9th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 505 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC