Else..if Problem

Discussion in 'PHP' started by qualityfirst, Dec 15, 2008.

  1. #1
    SORRY: Topic title should say "If...Else" problem.

    Hey.

    I have a minor else...if problem. I can't seem to figure out what's wrong, but maybe if someone else took a fresh look at it?

    The problem is that the form handler displays blank even when the form is filled out correctly.

    Here is register.php

    <?php 
    session_start();
    require("includes/config.php");
    ?>
    <html>
    <body>
    <head><title>Register for <?php echo $siteTitle; ?></title></head>
    <?php
    require("includes/functions.php");
    /* Make the user fill out the form if they haven't already done so */
    if (!isset($_POST["firstName"]) or !isset($_POST["lastName"]) or !isset($_POST["username"]) or !isset($_POST["password"]) or !isset($_POST["email"])){
    echo '<form action="register.php" method="post">
    First Name: <input type="text" size="15" name="firstName"></input> Last Name: <input type="text" size="15" name="lastName"></input><br />
    Username: <input type="text" size="30" name="username"></input><br />
    Password: <input type="password" size="20" name="password"></input><br />
    Email: <input type="text" size="50" name="email"></input><br />
    <input type="submit" value="SIGN UP!"></input>
    </form>';
    } else {
    /* If the user is here, it means they have filled out the form. Now we can begin to insert them into the database */
    require("includes/mysql_connect.php");
    $firstName = $_POST["firstName"];
    $lastName = $_POST["lastName"];
    $username_user = $_POST["username"];
    $password_user = $_POST["password"];
    $emailadd = $_POST["email"];
    /* Check if all data is valid */
    if (!filter_var($emailadd, FILTER_VALIDATE_EMAIL) or !ereg("^A-Z.*",$firstName) or !ereg("^A-Z.*",$lastName) or !ereg("....",$username_user) or !ereg("......",$password_user)){
    if (!filter_var($emailadd, FILTER_VALIDATE_EMAIL))
    echo "Please enter a valid email address!";
    if (!ereg("^[A-Za-z' -].*",$firstName))
    echo '<br>Please enter a valid first name!';
    if (!ereg("^[A-Za-z' -].*",$lastName))
    echo '<br>Please enter a valid last name!';
    if (!ereg("....",$username_user))
    echo '<br>Please enter a valid username. Must be a minimum of 4 characters long!';
    if (!ereg("......",$password_user))
    echo '<br>Please enter a valid password. Must be a minimum of 6 characters long!';
    } else {
    $enterdata = "INSERT INTO members (firstName,lastName,username,password,email,admin) VALUES ('$firstName','$lastName','$username_user','$password_user','$emailadd',0)";
    if (!mysql_query($enterdata,$con)){
    die("Error: " . mysql_error());
    } else {
    echo "You have successfully signed up!";
    $_SESSION['userid'] = grabuserid($username_user,$password_user);
    recordip($_SERVER['REMOTE_ADDR'],$_SESSION['userid']);
    }}}
    ?></body></html> 
    PHP:
    Thanks!
     
    qualityfirst, Dec 15, 2008 IP
  2. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is the title displaying correctly?

    Also, you should test each of your eregs, individually.

    Also, you should put more spaced out brackets
    if( ) {

    } else {

    }

    Instead of squishing them together and making it hard on yourself.
     
    Tarkan, Dec 15, 2008 IP
  3. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Hi, you're right, you will see it yourself after sleep.
    This line :
    if (!filter_var($emailadd, FILTER_VALIDATE_EMAIL) or !ereg("^A-Z.*",$firstName)
    is actually searching sequence A-Z not some capital letter, you have it correct later - ereg("^[A-Za-z' -].*",$firstName)
     
    lp1051, Dec 15, 2008 IP
  4. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When you filter name it should be like this:
    if(ereg("[a-zA-Z0-9 '-]+")
    This means it can be any of the characters inside the brackets.

    but when you do this:
    .* in yours, it means anything can fit there, so it won't matter.
    and
    ^[] this means beginning of the word.
    Did you mean [^A-Z] that would mean "NOT A-Z".
     
    Tarkan, Dec 16, 2008 IP
  5. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Thanks!!

    I knew it was some stupid mistake.

    +rep to all!

    PS: No the title isn't displaying...
     
    qualityfirst, Dec 16, 2008 IP