php - Insert data if not exist -


i have 2 tables cw_users , ref_users, both have column named id.

i'm using isam can't use foreign key. wanted insert id cw_users ref_users if didn't exist.

this did, didn't help:

$id = $_session['id']; $ref_code=md5($id);  mysql_query("insert ref_users (id) values ('$id') not exists (select cw_users id='$id')");  

the correct syntax insert ignore into

insert ignore ref_users (id) values ('$id') 

it insert if value not exist, , ignore statement if does.

note work if id primary key

edit: seems comments better off using on duplicate key update.

try query

insert ref_users(id, ref_code) values ('$id', '$ref_code') on duplicate key update ref_code = '$ref_code' 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -