Insert data when empty and update it when data exist

With so many function provided in mysql plus lazy, It seem like finding gem for replace my old approach 🙂

duplicate data isn’t good, I must search for exisiting data, in general : insert data to table when no data with that value exist and update table if data exist

For example :

I have table A with field :

1. id, integer 11 primary key auto increment

2. name, varchar 25

3. address, varchar 100

4. phone, varchar 20

5. username, varchar 10 unique

a common operation to insert data is :

Checking for data, ex :

$check_data = mysql_query("select * from A where username =’$user’");
if(mysql_num_rows($check_data)>0) {
do update data
mysql_query("update A set bla..bla..bla");
}
else {
insert query here
mysql_query("insert into A bla..bla..bla");
}

after find "replace()"

mysql_query("replace into A (field1,field2)  values(‘values1′,’values2’)")

Yes, I still need to do research for another ‘glitch’ possibility in current application.

From mysql manual :

As long index exist for that table, like primary key or unique for helping mysql to find duplicate data or not.

Yes, replace don’t insert please  😉