unemployment Posted October 10, 2012 Share Posted October 10, 2012 Below is a function to add a user to my database that signs in from facebook. What I don't understand is why my on duplicate key update isn't working. When a user signs in from facebook it continues to add in rows in the DB for that user. Please advise. public function facebook_add_user($uid, $email, $name, $oauth_provider) { $uid = (int)$uid; $email = mres(trim($email)); $name = mres(trim($name)); $oauth_provider = (int)$oauth_provider; if (strlen($email) >= 6) { $sql = "INSERT INTO `users` ( `oauth_provider`, `oauth_uid`, `name`, `email` ) VALUES ( '${oauth_provider}', ${uid}, '${name}', '${email}' ) ON DUPLICATE KEY UPDATE `oauth_uid` = VALUES(oauth_uid)"; $q = sql::q($sql); } $sql = "SELECT `id`, `email`, `name`, `oauth_uid`, `oauth_provider` FROM `users` WHERE `oauth_uid` = '${uid}' AND `oauth_provider` = 1"; $q = sql::q($sql); return sql::f_assoc($q); } Quote Link to comment https://forums.phpfreaks.com/topic/269319-on-duplicate-key-update-fails/ Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 How do you expect the database to know what a duplicate is? You need to create a UNIQUE index on the table so the database can actually define the word "duplicate" Also, oauth UUIDs are usually alphanumeric, you don't quote yours. You also have your quotes wrong, inside "quotes variables are {$likeThis}, not ${likeThat}" Quote Link to comment https://forums.phpfreaks.com/topic/269319-on-duplicate-key-update-fails/#findComment-1384257 Share on other sites More sharing options...
requinix Posted October 10, 2012 Share Posted October 10, 2012 You also have your quotes wrong, inside "quotes variables are {$likeThis}, not ${likeThat}" Alternate syntax, it's valid albeit practically never used. Quote Link to comment https://forums.phpfreaks.com/topic/269319-on-duplicate-key-update-fails/#findComment-1384260 Share on other sites More sharing options...
ManiacDan Posted October 10, 2012 Share Posted October 10, 2012 Valid the same way the while/endwhile is valid. It's still not the "right" way to do things. It's like we work in Perl sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/269319-on-duplicate-key-update-fails/#findComment-1384266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.