Hi everybody,
this is a very common newbie problem. The nature of numerous problems
requires inserting a new record into a table if it does not exists and
yet updating it if there is already one. As of MySql 4.1.0 it is
possible to render the following query:
INSERT INTO statistics (counter) VALUES (1) ON DUPLICATE KEY UPDATE
counter=VALUES(counter)+1
This is actually a correct/optimized implementation of a REPLACE query
as it works around the DELETE/INSERT race condition.
Best Regards,
Lucas
Shawn Wilson <shawn@glassgiant.com> wrote in message news:<414F1194.838AFBDB@glassgiant.com>...[color=blue][color=green]
> > As you can see, if there are no rows, I'd like to "insert", if not,
> > "update". Problem here is, the code somehow does not go inside else
> > block. And I echoed the mysql_affected_rows()'s value. It gives me -1.
> > What am I doing wrong? How do I fix it?[/color]
>
> I don't know what the problem is with your code, but you might consider using
> REPLACE instead of 2 INSERT/UPDATE queries.
>
> Read the comments here for more information:
>
http://ca3.php.net/manual/en/functio...ected-rows.php
>
>
> Shawn[/color]