ChrisMartino Posted August 12, 2010 Share Posted August 12, 2010 Hello there, I have a problem where the following code is running the query before it should. private function isAccountRegistered($AccountUsername, $AccountEmail) { global $Class; if($Class['MySQL']->currentRows("SELECT * FROM xhost_accounts WHERE account_username = '".$Class['MySQL']->parseClientInput($AccountUsername)."'") == 1) { return USERNAME_REGISTERED; } else if($Class['MySQL']->currentRows("SELECT * FROM xhost_accounts WHERE account_email = '".$Class['MySQL']->parseClientInput($AccountEmail)."'") == 1) { return EMAIL_REGISTERED; } } public function createClientAccount($AccountUsername, $AccountPassword, $AccountEmail, $AccountRealname) { global $Class; if($this->isAccountRegistered($AccountUsername, $AccountEmail) == USERNAME_REGISTERED) { echo("<div class=\"warning\">Unfortunately this username is in use by another member!, please select another!.</div>"); } else if($this->isAccountRegistered($AccountUsername, $AccountEmail) == EMAIL_REGISTERED) { echo("<div class=\"warning\">Unfortunately this email address is in use by another member!, please select another!.</div>"); } else { if($Class['MySQL']->Query("INSERT INTO xhost_accounts (account_username, account_password, account_email, account_realname, account_regdate, account_regtime, account_level, account_balance, account_notifications) VALUES('".$Class['MySQL']->parseClientInput($AccountUsername)."', '".md5($Class['MySQL']->parseClientInput($AccountPassword))."', '".$Class['MySQL']->parseClientInput($AccountEmail)."', '".$Class['MySQL']->parseClientInput($AccountRealname)."', '".date("d/m/y")."', '".time()."', '0', '0.00', 'Enabled')")) { $Class['Core']->refresh('index.php'); } else { echo("<div class=\"warning\">Unfortunately something went wrong there, please try again!.</div>"); } } } For some reason when the code above is run it executes if($Class['MySQL']->Query("INSERT INTO xhost_accounts (account_username, account_password, account_email, account_realname, account_regdate, account_regtime, account_level, account_balance, account_notifications) VALUES('".$Class['MySQL']->parseClientInput($AccountUsername)."', '".md5($Class['MySQL']->parseClientInput($AccountPassword))."', '".$Class['MySQL']->parseClientInput($AccountEmail)."', '".$Class['MySQL']->parseClientInput($AccountRealname)."', '".date("d/m/y")."', '".time()."', '0', '0.00', 'Enabled')")) Before it runs: if($this->isAccountRegistered($AccountUsername, $AccountEmail) == USERNAME_REGISTERED) { echo("<div class=\"warning\">Unfortunately this username is in use by another member!, please select another!.</div>"); } Causing it to say that the username is registered every time yet still inserting the account into the database, its returning that its registered because it inserts it before doing the check, does anybody have any idea why, this really has confused me, thanks for your time. Link to comment https://forums.phpfreaks.com/topic/210512-code-running-before-it-shouldnt/ Share on other sites More sharing options...
linus72982 Posted August 12, 2010 Share Posted August 12, 2010 It might be a problem with the quotes, though I didn't look too deeply, try putting quotes around every instance of EMAIL_REGISTERED and USERNAME_REGISTERED. Right now it appears you are returning constants instead of strings. If that isn't the problem, you have to realize that what the script is doing is telling you exactly where the problem is. It is telling you that: $this->isAccountRegistered($AccountUsername, $AccountEmail) == USERNAME_REGISTERED and: $this->isAccountRegistered($AccountUsername, $AccountEmail) == EMAIL_REGISTERED are both false so it is running the else portion every time. To test this theory, put this as your else: else {echo 'This is where the problem is'; die(); } If you see that text when you run it, it means what i said above is true. This tells us the problem with your code is either in the syntax of your if and elseif or, more likely, your code in the isAccountRegistered function is bad in some way. Link to comment https://forums.phpfreaks.com/topic/210512-code-running-before-it-shouldnt/#findComment-1098378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.