我正在尝试更新SQL数据库中的用户详细信息,但是如果该用户的一行不存在,我需要为该用户创建一行(每个用户只有一个) 它只是不更新或插入。 以下是以下脚本。
$sql = "IF EXISTS (SELECT * FROM user_details WHERE user_id = $user_id)
UPDATE user_details SET work = $work, education = $education, location = $location, relationship = $relationship, phone = $phone, email = $email, website = $website, language = $language, skill = $skill, tumbkr = $tumblr, instagram = $instagram, skype = $skype, facebook = $facebook, youtube = $youtube, twitter = $twitter, about = $about, user_id = $user_id WHERE user_id = $user_id
ELSE
INSERT INTO user_details (work, education, location, relationship, phone, email, website, language, skill, tumblr, instagram, skype, facebook, youtube, twitter, about, user_id)";
$q = $conn->prepare($sql);
$q->execute();
答案 0 :(得分:1)
执行以下操作:
INSERT INTO table_name (field1,field2) VALUES (val1,val2)
ON DUPLICATE KEY UPDATE field_name=VALUE;