0

i have a mysql table with columns like this

id
userid
userName
role

id is primary key, userid is unique

so i want to insert data to mysql from phplike this

if userid(in this case 11) is not in the db

INSERT INTO tblName (userid,userName,role) VALUES ('11','name','admin')

if this userid is exist in db then update username and role

UPDATE tblName SET userName = 'name', role = 'admin' WHERE userid=11;
Ba Ta
  • 43
  • 7

2 Answers2

0
INSERT INTO users (userid,userName,role,userPassword,device) VALUES ('11','21','31','41','51') 
ON DUPLICATE KEY UPDATE role='0'
arlind
  • 165
  • 1
  • 3
  • 15
0

In your case query will be:

INSERT INTO tblName (userid, userName, role) VALUES ('11', 'name', 'admin')
 ON DUPLICATE KEY UPDATE userName = 'name', role = 'admin';
Evgeny A. Mamonov
  • 1,576
  • 9
  • 11