in reply to Database Update or Insert

Hmm..
Why not use REPLACE instead of UPDATE+INSERT?
The only problem with REPLACE is when you UPDATE not all fields in table, but for your example REPLACE is good.
$dbh->do("REPLACE INTO $table SET item_id=?, upc=?", undef, $item_id, $upc) or die $dbh->errstr;

Replies are listed 'Best First'.
Re: Why not Replace?
by asdfgroup (Beadle) on Apr 27, 2002 at 13:57 UTC
    This won't work because of 2 resons :

    1) This will work only for key field but this is not clear based on example item_id is keyfield

    2) record can have additional fields. All this fields after replace will be empty