bytesize Posted November 2, 2010 Share Posted November 2, 2010 I need help making $_SESSION['code'] match variable $code when executed. They're both accessing rand() but with different results. <?php function create_user($params) { db_connect_posts(); $code = rand(11111111,99999999); $_SESSION['code'] = $code; $query = sprintf("INSERT INTO users SET users.screen_name = '%s', users.user_email = '%s', users.user_pwd = '%s', users.image = '%s', created_at = NOW(), users.code = $code, users.active = '0'" , mysql_real_escape_string($params['screen_name']), mysql_real_escape_string($params['user_email']), md5($params['user_pwd']), mysql_real_escape_string($params['image']) ); $result = mysql_query($query); if(!$result) { return false; } else { return true; } } ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 What?!?! $code = rand(11111111,99999999); $_SESSION['code'] = $code; echo $_SESSION['code'] .'<br>'.$code; Quote Link to comment Share on other sites More sharing options...
bytesize Posted November 2, 2010 Author Share Posted November 2, 2010 '$code = rand(11111111,99999999)' is inserted into the database with 'users.code = $code' and '$_SESSION['code']' is using '$code = rand(11111111,99999999)' as well. The INSERT 'users.code = $code' and $_SESSION['code'] return different results. They must match. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 2, 2010 Share Posted November 2, 2010 What?!?! $code = rand(11111111,99999999); $_SESSION['code'] = $code; echo $_SESSION['code'] .'<br>'.$code; What did that give you? Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 2, 2010 Share Posted November 2, 2010 I'm confused as well. In the script above you are generating a random number and 1) creating a new record in the database and setting one of the fields to that value and then 2) setting a session variable to the same value. You are saying the two values do not match. Exactly "WHERE" do they not match? You are obviously not comparing the two variables in the above script, so I must assume you are seeing the problem on another page. I will further assume you are storing the code as a session variable so you can grab the user's data from the database on subsequent page loads. So, are your queries returning no records on subsequent pages (i.e. no record is returned)? I don't see you starting the session in the above code. Don't know if that is the problem or if you jest left that off for brevity. Basically, what you are asking makes no sense within the context of the code you have provided. Quote Link to comment Share on other sites More sharing options...
bytesize Posted November 2, 2010 Author Share Posted November 2, 2010 The session is started elsewhere. The session is being used on register.php page that inserts into the database. When the query is echoed from user.php, which is the supplied code above, and the form is echoed from register.php the results are different. I can supply more code if necessary. Quote Link to comment Share on other sites More sharing options...
bytesize Posted November 2, 2010 Author Share Posted November 2, 2010 Problem solved. The create_user($params) function on the register.php page was being called after the session. Thank you for everyone's help, it is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.