Jump to content

Undefined variables...but they are.


erics1024

Recommended Posts

Here's my code. Basically, it takes the contents of the referring form and checks to see the username exists, and see if the passwords match. If they do, it creates the account and says it did. If not, it fails and does not create the account. I'm new to php so I'm sure this isn't the most efficient way of doing this. I've tried using single and double quotes in the _posts but neither fixes the problem. Also, when I submit the form, it adds the user to the database no matter what, bypassing all of the checks.

 

<?php

//GRAB THE VARIABLES FROM THE FORM

$username = $_POST["username"];

$desiredpass = $_POST["password1"];

$verifypass = $_POST["password2"];

$email = $_POST["email"];

?>

 

<?php require_once('Connections/d2tradesite.php'); ?>

 

<?php // Check to see if account already exists

mysql_select_db($database_d2tradesite, $d2tradesite);

$result=mysql_query("SELECT count(*) as numrecords FROM users WHERE username='" . $username . "'");

$row=mysql_fetch_assoc($result);

if ($row['numrecords'] >= 1) $ugood = 0;

else { $ugood = 1;}

if ($ugood = 1) checkpass();

 

?>

<?php

//check to see if passes match; if so, add db entry

function checkpass(){if ($desiredpass == $verifypass) $pgood = 1; else $pgood = 0;

}

?>

 

<?php

$registerdate = date('Y m d');

$addit = MYSQL_QUERY("INSERT INTO users (username,password,registerdate,email) VALUES ('$username', '$desiredpass', '$registerdate', '$email')");

function addtodb() { if ($ugood + $pgood == 2) {echo 'account worked';

$addit;}

else {echo 'account did not work';}

}

  ?>

 

Here's the apache error log:

 

[Fri May 09 22:47:33 2008] [error] [client 192.168.1.3] PHP Notice:  Undefined variable: verifypass in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\d2tradesite\\accountreg.php on line 22, referer: http://ggold01/d2tradesite/register.php

[Fri May 09 22:47:33 2008] [error] [client 192.168.1.3] PHP Notice:  Undefined variable: desiredpass in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\d2tradesite\\accountreg.php on line 22, referer: http://ggold01/d2tradesite/register.php

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/
Share on other sites

not sure, but you had an "=" where you needed "==":

<?php
//GRAB THE VARIABLES FROM THE FORM
$username = $_POST["username"];
$desiredpass = $_POST["password1"];
$verifypass = $_POST["password2"];
$email = $_POST["email"];
?>

<?php require_once('Connections/d2tradesite.php'); ?>

<?php // Check to see if account already exists
mysql_select_db($database_d2tradesite, $d2tradesite);
$result=mysql_query("SELECT count(*) as numrecords FROM users WHERE username='" . $username . "'");
$row=mysql_fetch_assoc($result);
if ($row['numrecords'] >= 1) $ugood = 0;
else { $ugood = 1;}
if ($ugood == 1) checkpass();

?>
<?php
//check to see if passes match; if so, add db entry
function checkpass(){if ($desiredpass == $verifypass) $pgood = 1; else $pgood = 0;
}
?>

<?php
$registerdate = date('Y m d');
$addit = MYSQL_QUERY("INSERT INTO users (username,password,registerdate,email) VALUES ('$username', '$desiredpass', '$registerdate', '$email')");
function addtodb() { if ($ugood + $pgood == 2) {echo 'account worked';
$addit;}
else {echo 'account did not work';}
}
     ?>

The problem is, you are not checking whether the form variables are set.

 

The reason the error is showing for you is because of your error reporting level.

 

Try putting this at the beginning of your script:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

 

This will only show errors which affect your script, ignoring notices.

mraiur, using the global keyword in a function defeats one of the main purposes of writing a function and you might as well just copy/paste the code every place it is called if you need to use the global keyword to make it work.

 

micmania1, in this thread the undefined variable message is occurring because the code is incorrect and will not work until the problem is corrected. Well written and tested code does not generate errors, warnings, or notices. Notice messages do affect your code in two ways - they are telling you that php found something wrong as it was executing your code (incorrect code, a typo in a variable name...) and the error response code that php executes every time it encounters a problem that generates a notice takes 20-30 times longer to execute than if the code was written correctly in the first place. The error reporting is just the last step in the error response code. Turning off notice messages still causes php to executed the 20-30 time longer error response code every time it encounters a problem in a program.

 

Edit: Here is a bit more on the subject of error reporting. On a live server you want to set display_errors off so that any un-caught and un-expected conditions in your code that do generate warnings and notices are not displayed. But, you do NEED error_reporting set to E_ALL so that these un-caught and un-expected conditions are logged so that you have a record of what is happening, such as a hacker attempt to break into your code or you have a logic problem that occurs for some un-expected type of data that is being entered and you need to modify your code so that you page will work for that data...

Archived

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

×
×
  • Create New...

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.