David
David

Reputation: 184

How can I update a user in my DB after a purchase?

<?php

require_once ('vendor/autoload.php');
require_once 'C:\xampp\htdocs\blog\core\init.php';

$_SESSION['user_id'] = 10;

$stripe = array(
"secret_key" => "sk_test_vDYOBYxCErhrgGCeWQJhR4mQ",
"publishable_key" => "pk_test_4mIuE3OsajKO4cnFppcDDISu"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);

$db = new PDO('mysql:host=127.0.0.1;dbname=blog', 'root', '******');

$userQuery = $db->prepare("
SELECT id, username, email, premium
FROM users
WHERE id = :user_id
");

$sql = "UPDATE users
    SET premium = 1
    WHERE id = :user_id";

$userQuery->execute(['user_id' => $_SESSION['user_id']]);

$user = $userQuery->fetchObject();

?> 

How can I update a certain user after they purchase premium?

This is the only way I could get it to work.

I want to update the user by username or email, but I'm failing. I want it to pull the email/username of the account signed in during a purchase, so it'll update in my database right after & won't update the wrong person, or worse everyone!

Upvotes: 2

Views: 93

Answers (1)

Capricorn
Capricorn

Reputation: 2089

The following code will set the variable "premium" to 1 based on a given e-mail address. Note that e-mail needs to be unique, otherwise all users with that e-mail address will become premium

$sql = "UPDATE users
SET premium = 1
WHERE email = :user_email";

$userQuery->execute(['user_email' => $theEMailAddressOfTheUserThatPurchasedPremium]);

Upvotes: 3

Related Questions