Originally posted by guest
Well, all i wanna do is insert first time and update the rest.
like
if (Insert == error_duplicate) {
update instead
}
What you may want is REPLACE instead of INSERT: http://dev.mysql.com/doc/mysql/en/REPLACE.html
"REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record for a PRIMARY KEY or a UNIQUE index, the old record is deleted before the new record is inserted."
This is much simpler if there is no additional data in the existing record that you need to conserve. If there is then you can the On DUPLICATE KEY form of the INSERT query:
"If you specify the ON DUPLICATE KEY UPDATE clause (new in MySQL 4.1.0), and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and already contains the value $var1, the following two statements have identical effect:
mysql> INSERT INTO table (a,b,c) VALUES ($var1,$var2,$var3)
-> ON DUPLICATE KEY UPDATE b=$var2,c=$var3;
mysql> UPDATE table SET b=$var2,c=$var3 WHERE a=$var1;" http://dev.mysql.com/doc/mysql/en/INSERT.html
If a=$var1 exists then an update is accomplished, if it does not exist then the insert is accomplished.
The second solution allows for updating of selective columns so that things like date inserted, last accessed, etc can be preserved.