Jump to content

How to update users in the DB by their email


Burtonn

Recommended Posts

I'm trying to get a users email so it will update them when they purchased premium, I'm using Stripe as my payment system. The only way I can get it to work, is if I insert their email/id into my php page. I'm wanting the page to get the users email on it's on.

 $_SESSION['user_id'] = 9;
 
$stripe = array(
    "secret_key" => "sk_test_rDYOBYxCErhrgGCeWQJhR4mQ",
    "publishable_key" => "pk_test_3mIuE3OsajKO4cnFppcDDISu"
    );
 
    \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
    ");
 
    $userQuery->execute(['user_id' => $_SESSION['user_id']]);
 
    $users = $userQuery->fetchObject();

I'm using ID's right now, I have to enter them on my own. The website isn't live, just testing. I can get the users email with Stripe on my checkout page by putting

echo $users->email

& the email shows up on the purchase screen, where normally they would have to type it. I'm not sure how to get it on purchase so it will update the user in the DB though.

Link to comment
Share on other sites

remember you need to fallow the same query style all the time so if you have something like 

 

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

 

for the update you have to add the email the same way 

 

$userQuery->execute(['user_id' => $_SESSION['user_id'], 'user_email' => 'user@user.com']);

 

and use the :user_email in your query

Link to comment
Share on other sites

remember you need to fallow the same query style all the time so if you have something like 

 

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

 

for the update you have to add the email the same way 

 

$userQuery->execute(['user_id' => $_SESSION['user_id'], 'user_email' => 'user@user.com']);

 

and use the :user_email in your query

I'm trying to use, for example: user_email, to automatically get their email, or the get the email displayed on the purchase screen. I have stripe setup to auto display the users email they're logged in with... Once they purchase I want my DB to update the user that has the email shown on the purchase screen/the one they're logged in with... For now I have to say, another example: $_SESSION['user_id'] = '1'; or $_SESSION['user_email'] = idk@whatever.com..... Instead of me typing the users id in or the users email I want it to get it itself, like $_SESSION['user_email'] = 'users_email' or something like that but I can't get it to work and I need help with what all I should do because nothing is working for me on this :confused:

Link to comment
Share on other sites

can you post more code where you are having the issue ... also try setting $_SESSION['user_email'] = $users->email; on the page where you can get the email to display

This is the only part I'm having problems with, I do not have anything getting the users email other than the $usersQuery / $users fetch code above, someone suggested I should use make a class, but I don't understand because I can get the users email to show up on the stripe purchase screen when they click the premium link, this is the code on my checkout page, which is the page that I can get to show the email "stripes purchase screen"

<form action="charge.php" method="post">
	<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
		data-key = "<?php echo $stripe['publishable_key']; ?>"
		data-name =  "Blog"
		data-description = "Premium"
		data-currency = "usd"
		data-email = "<?php echo $users->email; ?>"
		data-amount = "1000"> // amount in cents
        </script>
</form>

I just can't get it to update going by the users email, I'm not new to php but I'm also not amazing with it. I could be using my code wrong, but I've tried everything.. Except the right thing haha I also could leaving a bit out, I'm not sure, but that's why I'm here. Trial & Error ::)

Link to comment
Share on other sites

can we see the code for the update query...

 

The above code is all I have that has to do with what I'm doing, not including all the composer files and my charge & checkout page, but they don't have anything to do with my db, just the purchase, like I said I'm not too good with php. So I could be missing a bit, let me know if I am & what, please!

Link to comment
Share on other sites

at this point im still not fallowing if you can get the email to show isant that what you want? what needs to update based on the users email?

 

I can get the email to show for Stripe, when a user clicks the premium link at the bottom of my page, a Stripe purchase window comes up. The window will ask for the users email, credit/debit card info.

 

I have setup the Stripe purchase window to where the user doesn't have to enter their email, it automatically has their accounts email typed out for them.

I completed that task by echoing the users email in the code above, you can see where I did that on the <form> code I posted last.

 

I now want the user to be updated when they purchase premium. I want to update them in the database by their email, considering their email will be unique.

So I need help with doing this.

 

The code I've posted above is updating users in the database by their ID, the only problem I have with that is, the only way it'll update the user by their ID is if I type the users ID at the top in SESSION. It goes the same with the email.

 

I don't care if it updates them to premium by finding their email or ID in the database, but I want to set it where it updates that user after they purchase and only that user, without me having to type in their user id or email in the session.

 

Basically, I want to be able to put, $user->email, or something like that and define all users, then update users in the database depending on their email, after they purchase . The only way I can update the user by email or ID is by typing it in the session myself.

Link to comment
Share on other sites

if the user is logged int o your site there must be a session or cookie for the use that you can call on on any page to get there info and then run an update query but you would also need your payment system to post that info back to you if not unless it open in a new window then closes.  there should be a confirm or purchases summary page that you can edit ...

Link to comment
Share on other sites

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.