Jump to content

Archived

This topic is now archived and is closed to further replies.

robcrozier

vaiable access

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Is this assignment in a function? Can you post the rest of the script?

Ken

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.