0

Ok So I have a function that is executed after a customer purchases a digital code. The function is very simple and uses pdo after I thought that would fix it instead of using mysqli. everything works but the username variable does not get put in the table even thought I can echo it out and use it in other sql command just fine but for some reason not this particular line.

Here is the code:

function redeemToken($amount = 1){
    $db_tokens = new PDO('mysql:host=IP;dbname=DB', "U", "P");
    $qOne = $db_tokens->query("SELECT * FROM tokens WHERE used='0' LIMIT $amount;"); //selects the first unused token
    $dataToken = $qOne->fetch();
    $token = $dataToken["token"];
    $qTwo = $db_tokens->query("UPDATE tokens SET used='1', usedby='".$php_session_username."', date_used=NOW() WHERE token='".$token."';"); //uses the token for the current logged in user
    $db_kvs = null;
}

As I stated everything is executed fine except for the fact that the username value ($php_session_username) does not get added into the table. but when echo'd out shows the logged in users name clearly as well as with any other sql command I run on that page; it works fine.

Any help would be much appreciated so thanks in advance.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

1 Answers1

2

I am not sure but I guess the problem is that $php_session_username is not the function's local variable and neither the function is getting it value in its parameter. Try something like this.

function redeemToken($amount = 1,$php_session_username)
{
//function body
}
($amount = 1,$php_session_username);
Haris
  • 764
  • 4
  • 9
  • 27
  • Yeah it was something like that. I was passing the session username to a variable and using the variable defined outside the brackets. I fixed it now. Thanks – Persuasion Feb 03 '17 at 16:03
  • If you find the answer useful, mark it as correct :) – Haris Feb 03 '17 at 16:14