erics1024 Posted May 10, 2008 Share Posted May 10, 2008 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 More sharing options...
jonsjava Posted May 10, 2008 Share Posted May 10, 2008 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';} } ?> Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-537317 Share on other sites More sharing options...
trq Posted May 10, 2008 Share Posted May 10, 2008 You need to take a look at scope. Variables declared outside of a function are not available within a function and vice versa. Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-537365 Share on other sites More sharing options...
mraiur Posted May 10, 2008 Share Posted May 10, 2008 declare the variables in the function like : function MyFUNCTION() { global $var1,$var2; } Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-537381 Share on other sites More sharing options...
micmania1 Posted May 10, 2008 Share Posted May 10, 2008 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. Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-537423 Share on other sites More sharing options...
PFMaBiSmAd Posted May 10, 2008 Share Posted May 10, 2008 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... Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-537560 Share on other sites More sharing options...
erics1024 Posted May 11, 2008 Author Share Posted May 11, 2008 PF, could you be so kind as to tell me why the code is incorrect and what I can do to make it correct? Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-538099 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2008 Share Posted May 11, 2008 Did you read thorpe's post and research variable scope in the php manual? Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-538106 Share on other sites More sharing options...
erics1024 Posted May 11, 2008 Author Share Posted May 11, 2008 Yes. I didn't quite understand what he was talking about until I googled it actually. I was under the impression you were rejecting all of the other user's suggestions. My bad. Link to comment https://forums.phpfreaks.com/topic/104969-undefined-variablesbut-they-are/#findComment-538129 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.