简体   繁体   中英

How to write this complicated MySQL Query

I have a table called Users and it has three columns Email , FB_Id , Google_ID

The question is how I can replicate following idea into just one MySQL query .

  1. Suppose someone login with FB_id then I should check in the DB if the email exist. If yes then update the related ID in this case FB_Id.

What I am trying to do is to keep all the id from different provider for a user. If a user has same user ID for google+ and Fb then we can have ID of both the provider for that email and my website will allow to login it in

The function that I have written is something Like this. Now I want to add the UPDATE action into this and then get the user details form the DB.

Function

function checkUpdate () {
    if ($this->email)
    {
       $query  =" SELECT * FROM `users` WHERE `Email` ='".$this->email."'";
    }
    else
    {
       echo "Email not exist ";
    }

    $rc = $this->parent->database->query($query);

    if (mysql_num_rows($rc)>0)
    {
        $this->flag_details = 'true';
    }

    while ($rfg=mysql_fetch_array($rc))
    {
        //print_r($rfg);

        $_POST['im_password'] = $rfg['Password'];
        $_POST['im_user'] = $rfg['Email'];
    }

}

Update1 .

function checkUpdate () {
    if ($this->email)
    {
        $query  =" SELECT * FROM `users` WHERE `Email` ='".$this->email."'";
    }
    else
    {
        echo "Email not exist ";
    }

    $rc = $this->parent->database->query($query);
    if (mysql_num_rows($rc)>0)
    {
        $this->flag_details = 'true';

        //Here is my logic for it which I want to make in one sql query 
        if (isset($_POST['im_FbId']))
        {
            $sql = "UPDATE `users` SET fb_id='".$_POST['im_FbId']."' WHERE id=2";
        }

        if (isset($_POST['im_GId']))
        {
            $sql = "UPDATE `users` SET google_Id='".$_POST['im_GId']."' WHERE id=2";
        }

        $this->parent->database->query($sql);
    }

    while ($rfg=mysql_fetch_array($rc))
    {
        //print_r($rfg);

        $_POST['im_password'] = $rfg['Password'];
        $_POST['im_user'] = $rfg['Email'];
    }

}

Thanks in Advance

UPDATE users SET FB_id ='FACABOOK_FB_ID'WHERE EMAIL ='FB_EMAI';

if i understand your question correctly you want to update both or one of google id and facebook id

<?php

$sql = "UPDATE `users` ";
$sql .= isset($_POST['im_FbId']) ? " SET fb_id='{$_POST['im_FbId']}' " : "";
$comma = isset($_POST['im_FbId']) ? "," : "";
$sql .= isset($_POST['im_GId']) ? " {$comma}SET google_Id='{$_POST['im_GId']}' " : "";
$sql .= " WHERE id=2";

$this->parent->database->query($sql);

if only one exists for example $_POST['im_FbId'] the $sql value will be

    UPDATE `users` SET fb_id='facebook id' WHERE id=2

if both Id's exists the $sql value will be

    UPDATE `users` SET fb_id='facebook id' ,SET google_Id='google id' WHERE id=2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM