...

View Full Version : preg_match problem.



SKY-ProToSs
05-30-2011, 10:24 PM
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

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($query, MYSQLI_ASSOC)) {

if ($query3 == 0 && 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";

}

?>

Ahlahn
05-31-2011, 12:19 AM
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?

SKY-ProToSs
05-31-2011, 02:13 AM
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?

Ahlahn
05-31-2011, 02:29 AM
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:



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

if ($query3 == 0 && 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";

}

SKY-ProToSs
05-31-2011, 02:55 AM
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:



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

if ($query3 == 0 && 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.

Ahlahn
05-31-2011, 02:57 AM
The above matches 1 or more characters. What are you trying to match exactly?

SKY-ProToSs
05-31-2011, 03:24 PM
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.

Ahlahn
05-31-2011, 08:21 PM
Hm. It's working for me...

Try this:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum