Jump to content


Photo

vaiable access


  • Please log in to reply
3 replies to this topic

#1 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 21 October 2006 - 03:21 PM

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:

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;
}

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!


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 October 2006 - 04:01 PM

Is this assignment in a function? Can you post the rest of the script?

Ken

#3 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 21 October 2006 - 04:11 PM

No, i'm simply assigining a variable with a random value at the top of the script like this:
$id = a;

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:
//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>";
						 }
				}
		
		}

Next, im trying to access the changed variable within another if statement further down the script, like this:
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);

						}


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 21 October 2006 - 04:20 PM

Your query might be failing. So change this:
$result = mysql_query ($query);
To this:
$result = mysql_query ($query) or die("error with query:<br />\n" . mysql_error());
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:
$row = mysql_fetch_array ($result, MYSQL_NUM); 

if($row){
To this:
if(mysql_num_rows($result) > 0)
{
    $row = mysql_fetch_array ($result, MYSQL_NUM);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users