| | |
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:
Solved Threads: 0
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...
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...
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";
}
$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";
}
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
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
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!
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!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Problems with PHP and CSS in IE 6.0
- Next Thread: PHP Help
Views: 505 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
access ajax apache archive array arrays beginner binary box broken buttons cakephp cart check checkbox class classes cms code curl database date development directory display download dropdown drupal dynamic echo email error file files folder form forms functions header href htaccess html image include insert ip java javascript joomla jquery limit link list login loop mail menu mlm mod_rewrite multiple mysql order output parse password paypal pdf php problem query radio redirect regex remote results script search select server session sessions sort source sql string table tutorial update updates upload url user validation validator variable video web website wordpress xml





