The exit command in my code works until I put in the line in red. Why is this? I'm trying to check if the user's inputed username is already taken and if it is output the error message at the input username field. Please help me. If you can't figure out the code I'm fine with a brand new code. My database stucture looks like this: "database name" --> Username --> username --> "values" This is in the head: // $MM_flag="MM_insert"; if (isset($_POST[$MM_flag])) { $loginUsername = $_POST['username']; $LoginRS__query = "SELECT username FROM UserNames WHERE username='" . $loginUsername . "'"; mysql_select_db($database_username, $username); $LoginRS=mysql_query($LoginRS__query, $username) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); //if there is a row in the database, the username was found - can not add the requested username if($loginFoundUser){ $errortext = "Username exists."; exit; } } and this is in the body: <?php if (isset($errortext)) { echo ("$errortext"); } ?> Thanks in advance. ~Imozeb
exit() terminates/kill's all execution after it. Furthermore you shoul'dnt really use it in a non-development environment, due to it causing issues in the long-run. Better finding an alternative method; if your just wanting to disabled everything if the username exists you could simply do. <?php $MM_flag="MM_insert"; if (isset($_POST[$MM_flag])) { $loginUsername = $_POST['username']; $LoginRS__query = "SELECT username FROM UserNames WHERE username='" . $loginUsername . "'"; mysql_select_db($database_username, $username); $LoginRS=mysql_query($LoginRS__query, $username) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); //if there is a row in the database, the username was found - can not add the requested username if($loginFoundUser){ $errortext = "Username exists."; } else { //do whatever you want, if the username doesn't exist, here... } } ?> PHP: <?php //and this is in the body: if (isset($errortext)){ echo($errortext); } ?> PHP:
I need the exit() command because it exits out of all the PHP script and after the check username function is the insert to database function. Please tell me how to make the exit() command work so that my script does not run the rest of the code after it.
That's what it does. It also means that $errortext will never be seen or used because once it exits, that's it, no more PHP on that HTTP request.
Okay, but to do that I'm going to have to rewrite most of my code because most of it is based on many different functions.
heres a bad solution, but one that should work. Before your exit() have php echo a javascript command wich displays the error via innerHTML then exit() u SHOULD really just rewrite your script.