Why won't the exit command work in my code

Discussion in 'PHP' started by Imozeb, Feb 6, 2010.

  1. #1
    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
     
    Imozeb, Feb 6, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    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:
     
    danx10, Feb 6, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Imozeb, Feb 6, 2010 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    SmallPotatoes, Feb 6, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, then is there any way I can break the script without losing all my variables?
     
    Imozeb, Feb 6, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    I showed in my first post use else.
     
    danx10, Feb 6, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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. :(
     
    Imozeb, Feb 6, 2010 IP
  8. hostgenius

    hostgenius Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    hostgenius, Feb 8, 2010 IP
  9. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Ya. I'm rewriting my code now. Thanks for all the help people! :)

    This thread is resolved.
     
    Imozeb, Feb 8, 2010 IP