tags:

views:

50

answers:

2

I can't for the life of me figure out why this function is causing multiple entries into my database table...

When I run the function I end up with two records stacked on top of each one second apart

screen cap

here is the function:

function generate_signup_token(){
    $connection = new DB_Connect(); // <--- my database connection class
    $ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']); 
    $sign_up_token = uniqid(mt_rand(), true);
    $_SESSION['signup_token'] = $sign_up_token;
    $sign_up_token = mysql_real_escape_string($sign_up_token);
    $query = "INSERT INTO `token_manager` (`ip_address`, `signup_token`) VALUES ('$ip', '$sign_up_token')";
    mysql_query($query);
}

generate_signup_token();
+3  A: 

It looks like your function is fine. It appears that you are somehow calling the function twice. I'd suggest adding some debugging information to the function to figure out why. The function debug_print_backtrace might come in handy for this.

Mark Byers
this is my result:#0 generate_signup_token() called at [/home/hupcap/public_html/mydomain.com/index.php:19] I'm assuming that's just once?
Jascha
@Jascha: I think it's being called once per request. Check your web server logs to see how many requests there are.
Mark Byers
@mark, yeah, I gave webbiedave the chck mark for the access log comment, I greatly appreciate your pointing out this debug function for me.
Jascha
+3  A: 

The biggest piece of evidence that this function is being called more than once is the different signup tokens that are being generated.

Also, there is a one second difference between inserts which may be indicative of multiple page requests. If there is consistently such a time disparity, I'd investigate the access log. If there's only one page request, then the function must be called more than once somehow.

webbiedave
+1: For check access log.
Mark Byers
I wish I could give you both the right answer check.. I threw in a die(); function after I called the function and it indeed inserted just one record in the DB. I'll have to go rethink my whole action list now...
Jascha
Glad I could help. I upvoted Mark's as well.
webbiedave