Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.