Ne.OnZ Posted May 24, 2008 Share Posted May 24, 2008 Hello, I have a simple question. I have 2 pages, one looks like this: for($i = 0; $i<10; $i++) { $temp = rand(0, 35); $code = "{$code}{$temp}"; } $_SESSION['key'] = $code; That works out perfectly. On the other page I have this: $key = $_SESSION['key']; "INSERT INTO users(username, password, email, rank, avatar, code) VALUES('$username', '$password', '$email', '1', 'http://divnx.net/images/no_avatar.gif', '$key')"; This is where the problem lies. It doesn't insert the $key into the database. I tried to set code in mysql to VARCHAR, CHAR, INT, and BIGINT. Still No luck. Any ideas to what may be wrong? Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/ Share on other sites More sharing options...
BlueSkyIS Posted May 24, 2008 Share Posted May 24, 2008 do you start each page with session_start()? Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549230 Share on other sites More sharing options...
Ne.OnZ Posted May 24, 2008 Author Share Posted May 24, 2008 Yes I Did. o.o Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549233 Share on other sites More sharing options...
BlueSkyIS Posted May 24, 2008 Share Posted May 24, 2008 is $key in the SQL if you echo it? if not, is $_SESSION['key'] set on the 2nd page? if not, we need to see more of the first page code. Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549235 Share on other sites More sharing options...
PFMaBiSmAd Posted May 24, 2008 Share Posted May 24, 2008 The posted code is invalid nor does it contain any mysql function calls. Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549238 Share on other sites More sharing options...
Ne.OnZ Posted May 24, 2008 Author Share Posted May 24, 2008 Yes if I echo $key, the string comes up. The session is set on the 1st page. I'll give you more code: 1st Page: for($i = 0; $i<10; $i++) { $temp = rand(0, 35); $code = "{$code}{$temp}"; } $_SESSION['key'] = $code; After you hit submit on the form: 2nd Page $key = $_SESSION['key']; function register($username, $password, $email, &$res) { $que = "INSERT INTO users(username, password, email, rank, avatar, code) VALUES('$username', '$password', '$email', '1', 'http://divnx.net/images/no_avatar.gif', '$key')"; $res = mysql_query($que) OR die(mysql_error()); } if($username && $password && $password2 && $email && $checkbox && $security == $_SESSION['key'] && !$set) { register($username, $password, $email, $res); } Hope this helps a bit more. Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549241 Share on other sites More sharing options...
.josh Posted May 24, 2008 Share Posted May 24, 2008 so...everything else inserts just fine, except $key, and if you do echo $que; it shows the query with all the values, including the value for $key? Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549249 Share on other sites More sharing options...
Ne.OnZ Posted May 24, 2008 Author Share Posted May 24, 2008 Oh, I didn't echo $que. EDIT: Echoed $que and it seems $key doesn't show up. o.o Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549259 Share on other sites More sharing options...
.josh Posted May 25, 2008 Share Posted May 25, 2008 well, $key comes from $_SESSION['key'] and $_SESSION['key'] comes from $code so echo $_SESSION['key'] and $code and post what happens edit: well, $key is on your 2nd page, so... are you sure you have session_start(); on the top of your page? And on the page where you initially set it? Because I see no reason (based on your code provided) why $code shouldn't contain info, which means your session variable is the problem. Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549261 Share on other sites More sharing options...
Ne.OnZ Posted May 25, 2008 Author Share Posted May 25, 2008 They both print out the string like they should. o.o Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549263 Share on other sites More sharing options...
.josh Posted May 25, 2008 Share Posted May 25, 2008 okay i got it. $key is being treated like a new, local variable because your query is inside your function. So you are either going to have to make $key a global variable when you assign the session var to it, or else pass it as an argument in your function. Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549264 Share on other sites More sharing options...
Ne.OnZ Posted May 25, 2008 Author Share Posted May 25, 2008 :o Thank You So Much! It works now! As you said I put global $key inside the function, and it works! Cheers! :) :) Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549271 Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2008 Share Posted May 25, 2008 Your function is already using parameters for $username, $password, $email, $res. Add $key as a parameter for consistency. A month from now or a year from now when you need to do anything with this code, you will find that consistency will make your life easier. Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549279 Share on other sites More sharing options...
Ne.OnZ Posted May 25, 2008 Author Share Posted May 25, 2008 Thank You for mentioning that! Will add it now. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/107131-solved-storing-session-in-mysql/#findComment-549290 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.