Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts

    preg_match problem.

    I'm having a problem with the preg_match() function.

    I can't figure out why it's not running that if statement. I know it's the preg_match, but if I try to use an existing username the proper if statement will go off, and it definitely connects and runs the query. So, what's wrong there? Thanks, Rob

    PHP Code:
    <?php

    if (!$_SESSION['user']) {

    ?>

        <form action="register.php" method="POST">
            <input type="username" value="username" id="regusername" name="regusername" />
            <input type="password" value="password" id="regpassword" name="regpassword" />
            <input type="text" size="40" value="Enter  your email" id="regemail" name="regemail" />
            <input type="text" size="60" value="Enter your address" id="regaddress" name="regaddress" />
            <input type="text" size="60" value="Line 2" id="regaddress2" name="regaddress2" />
            <input type="submit" value="Register" id="regsubmitted" name="regsubmitted" />
        </form>

    <?php

        
    if ($_POST['regsubmitted']) {

            
    $usercase $_POST['regusername'];
            
    $reguser strtolower($usercase);
            
    $passcase $_POST['regpassword'];
            
    $regpass strtolower($passcase);
            
    $emailcase $_POST['regemail'];
            
    $regemail stripslashes($emailcase);
            
    $addresscase $_POST['regaddress'];
            
    $addresscase2 $_POST['regaddress2'];
            
    $regaddress stripslashes($addresscase);
            
    $regaddress2 stripslashes($addresscase2);

            
    $regaddressboth $regaddress " " $regaddress2;

            
    $abc mysqli_connect('localhost','rsarfaty_2','fusiongtxo0') or die ("There was an error with the database!");
            
    mysqli_select_db($abc,'rsarfaty_main');

            
    $query mysqli_query($abc,"SELECT * FROM MEMBERS WHERE USERNAME ='" $usercase "'");

            
    $query3 mysqli_num_rows($query);

                while (
    $row mysqli_fetch_array($queryMYSQLI_ASSOC)) {

                    if (
    $query3 == && preg_match("[a-zA-Z9-0._-]",$usercase)) {

                        
    $query2 mysqli_query($abc,"INSERT INTO MEMBERS VALUES('$reguser','$regpass','$regemail','$regaddressboth')"
                        or die (
    "There was an error with the database!");

                        echo 
    "You have registered";

                    }

                    else {

                        if (
    $query3 == 1) {

                        echo 
    "The username already exists";

                        }
                    }
                }

        }
    }

    else {

        echo 
    "You are already logged in. Please logout to view this page";

    }

    ?>

  • #2
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    You left out delimiters for your regular expression? Try enclosing your regex with /. Also, your regular expression only matches a single character- did you forget to put quantifiers?

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Ahlahn View Post
    You left out delimiters for your regular expression? Try enclosing your regex with /. Also, your regular expression only matches a single character- did you forget to put quantifiers?
    Yes, what are quantifiers?

  • #4
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    The regex you have now only matches a single character. [a-z] matches a single letter between a and z, so we need a quantifier {} to specify the number of characters we wish to match. Another issue you have is that you have the number range backwards- it should be 0-9, not 9-0.

    This should work:

    PHP Code:
    //if you want to limit your characters to 20, just replace {1,} with {1,20}

    if ($query3 == && preg_match("/[a-zA-Z0-9\_\-\.]{1,}/",$usercase)) {

                        
    $query2 mysqli_query($abc,"INSERT INTO MEMBERS VALUES('$reguser','$regpass','$regemail','$regaddressboth')"
                        or die (
    "There was an error with the database!");

                        echo 
    "You have registered";

                    } 

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Ahlahn View Post
    The regex you have now only matches a single character. [a-z] matches a single letter between a and z, so we need a quantifier {} to specify the number of characters we wish to match. Another issue you have is that you have the number range backwards- it should be 0-9, not 9-0.

    This should work:

    PHP Code:
    //if you want to limit your characters to 20, just replace {1,} with {1,20}

    if ($query3 == && preg_match("/[a-zA-Z0-9\_\-\.]{1,}/",$usercase)) {

                        
    $query2 mysqli_query($abc,"INSERT INTO MEMBERS VALUES('$reguser','$regpass','$regemail','$regaddressboth')"
                        or die (
    "There was an error with the database!");

                        echo 
    "You have registered";

                    } 
    Now it will only work on the first character.

  • #6
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    The above matches 1 or more characters. What are you trying to match exactly?

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Ahlahn View Post
    The above matches 1 or more characters. What are you trying to match exactly?
    I'm trying to match the username entered with the regular expression, to check to see if it's valid. It only works if the first character is a special character.

    Not sure why.

  • #8
    New Coder
    Join Date
    Sep 2010
    Location
    The Twilight Zone
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Hm. It's working for me...

    Try this:

    /[a-zA-Z0-9\_\-\.]{1,}+/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •