Aaron Navies
Aaron Navies

Reputation: 1263

INSERT Multiple Database Values To Specific Rows

How can I INSERT the values of this function to only WHERE the value of the username column (located in the login table, contains the exact same value as the value of the column nickname (located in a table called active_users).

    $stmt = $this->pdo->prepare('INSERT INTO login (device_token) VALUES (?)');
    $stmt->execute(array($token));

Additional info: when a user logs in, I set up the API to add the user's name to both username (in the login table) and nickname in the active_users table. Since these values will always be the same for their respective tables, I was hoping we could add some conditional logic to achieve this type of specificity. My goal is to take the device_token parameter and pass it to a different spot in the database.

Edit: Schema

active_users table

login table

enter image description here

enter image description here

Update AFNetworking command for login

NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command", _nicknameTextField.text = fldUsername.text, @"username", hashedPassword, @"password", nil ];

Update: active_users INSERT statment

$stmt = $this->pdo->prepare('INSERT INTO active_users (user_Id, device_token, nickname, secret_code, ip_address) VALUES (?, ?, ?, ?, ?)');
$stmt->execute(array($userId, $token, $name, $code, $_SERVER['REMOTE_ADDR']));

Upvotes: 0

Views: 116

Answers (2)

Santiago
Santiago

Reputation: 714

If I understand you correctly. What you really want is an update and not an insert. You want to add the value of device_token to the row with nickname, correct?

You would do that with an update:

UPDATE login
SET device_token = @token
WHERE username = @nickname

If you're trying to update the record with an existing record from active_users table than:

UPDATE login AS L
JOIN active_users AU ON L.username = AU.nickname
SET L.device_token = AU.device_token

Though, looking at your tables. I'm wondering if you're repeating too much of the data. It seems like you can link login and active_users by user_id. Well, this would get off topic, so I'll just suggest double checking db design concepts and your needs.

Upvotes: 3

Andreas Schrammel
Andreas Schrammel

Reputation: 463

Using PDO it should be

UPDATE login
SET device_token = :token
WHERE username = :nickname

where the params array that will be bound looks like

$params = array("token" => $token, "nickname" => $nickname);

Full example:

$stmt = $this->pdo->prepare('UPDATE login SET device_token=:token WHERE nickname=:nickname');
$stmt->execute(array("token" => $token, "nickname" => $nickname));

Or you use the method of binding with ?

UPDATE login
SET device_token = ?
WHERE username = ?

where the parameter array is

$params = array($token, $nickname);

Full example:

$stmt = $this->pdo->prepare('UPDATE login SET device_token=? WHERE nickname=?');
$stmt->execute(array($token, $nickname));

Upvotes: 1

Related Questions