Jump to content

On Duplicate Key Update Fails


unemployment

Recommended Posts

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);
   }

Link to comment
https://forums.phpfreaks.com/topic/269319-on-duplicate-key-update-fails/
Share on other sites

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}"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.