robcrozier Posted October 21, 2006 Share Posted October 21, 2006 Ok, imp having a bit of a problem accessing a variable that is initialized at the top of the script and then its value changed within an if statement further down the script:[code]if($row){$form_1_valid = true;$id = $row[0]; // this is the variable in question, it’s an auto incremented id field from the database$form_2_display = true;}[/code]Now I try to access the $id variable near the bottom of the script and it contains no value???Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/24660-vaiable-access/ Share on other sites More sharing options...
kenrbnsn Posted October 21, 2006 Share Posted October 21, 2006 Is this assignment in a function? Can you post the rest of the script?Ken Quote Link to comment https://forums.phpfreaks.com/topic/24660-vaiable-access/#findComment-112330 Share on other sites More sharing options...
robcrozier Posted October 21, 2006 Author Share Posted October 21, 2006 No, i'm simply assigining a variable with a random value at the top of the script like this:[code]$id = a;[/code]Next, if the form on the page is submitted, the vlues are validated and if validation is ok, the value of id is changed to an integer value, like so (the $id variable is changed near the bottom on this piece of code:[code]//validate the first form if (isset($_POST['submit'])) { // check for a username if (eregi ("^[[:alnum:]_-]{1,50}$", stripslashes(trim($_POST['username'])))) { $u = trim($_POST['username']); } else { $u = false; echo '<p><font color = "red" size = "-2" face="Arial, Helvetica, sans-serif"> >>> Please enter a valid username.</font></p>'; } // check for a password if (eregi ("^[[:alnum:]]{1,50}$", stripslashes(trim($_POST['password'])))) { $p = trim($_POST['password']); } else { $p = false; echo '<p><font color = "red" size = "-2" face="Arial, Helvetica, sans-serif"> >>> Please enter a valid password.</font></p>'; } // If everything's OK if ($u && $p) { // attemt to find a match for the users dataials in the database $query = "SELECT id, username, password from users WHERE username='$u' AND password='$p'"; $result = mysql_query ($query); $row = mysql_fetch_array ($result, MYSQL_NUM); if($row){ // set variables $form_1_valid = true; $id = $row[0]; //THIS IS WHERE MY VARIABLE IS CHANGED!!! $form_2_display = true; } else { //display error message echo "<font color='red' face='arial' size='-2'>invalid username and password, pleas try again</font>"; } } }[/code]Next, im trying to access the changed variable within another if statement further down the script, like this:[code]if($result) { //insert ok echo 'id = '.$id; // delete old cookie, new one issued upon 1st login setcookie("username", "", time()-60*60*24*365); setcookie("password", "", time()-60*60*24*365); }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24660-vaiable-access/#findComment-112331 Share on other sites More sharing options...
wildteen88 Posted October 21, 2006 Share Posted October 21, 2006 Your query might be failing. So change this:[code]$result = mysql_query ($query);[/code]To this:[code]$result = mysql_query ($query) or die("error with query:<br />\n" . mysql_error());[/code]If you get any errors post them here in full. Do not post part of the error message. It is important you post the error as-is.Also I'd suggest you change this:[code]$row = mysql_fetch_array ($result, MYSQL_NUM); if($row){[/code]To this:[code]if(mysql_num_rows($result) > 0){ $row = mysql_fetch_array ($result, MYSQL_NUM);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/24660-vaiable-access/#findComment-112334 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.