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; } } ?> Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/ 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; Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129593 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. Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129600 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? Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129603 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. Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129605 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. Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129609 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. Link to comment https://forums.phpfreaks.com/topic/217588-query-does-not-match-session/#findComment-1129622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.