Log in

View Full Version : patterning matching issues



SirTom909
04-15-2009, 01:21 PM
Hey Everyone,

Having some trouble with ereg function in my PHP script. The user should be able to query the db either by the ID number or surname, however I want to apply some patterning matching to prevent a user from querying a single number or digit and have a large number of entries returned when searching by ID.

The ID appears in four ways, "ABCD123" "ABCD12" and "AB123" "AB12" now I want a user to be able to query using any of the those four examples, however my script doesnt appear to be working, any help would be great.



<body>


<form name="search.php" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="id">ID Number</option>
<Option VALUE="surname">Surname</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?

if ($searching =="yes")
{
echo "<h2>Results</h2><p>";


if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}


mysql_connect('localhost', 'user', 'pword') or die(mysql_error());
mysql_select_db("server") or die(mysql_error());


function checkfind($find) {
$find = strtoupper(str_replace(chr(32),'',$find));
if (ereg ("^(AB)([0-9])|^(ABCD)([0-9])",$find));

return $find;
else
return FALSE;

}


$find = strip_tags($find);
$find = trim ($find);
$find = str_replace(' ','',$find);


$data = mysql_query("SELECT * FROM officers WHERE upper($field) LIKE'%$find%'");


while($result = mysql_fetch_array( $data ))
{
echo $result['id'];
echo " ";
echo $result['surname'];
echo "<br>";
echo $result['url'];
echo "<br>";
echo $result['expiry'];
echo " ";
echo "<br>";
}


$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}


echo "<b>Searched For:</b> " .$find;
}
?>

</body>
</html>

Master_script_maker
04-15-2009, 02:16 PM
try:

ereg ("^(AB[0-9]{2,3}|ABCD[0-9]{2,3})",$find)

SirTom909
04-16-2009, 11:21 AM
thank you very much for your help, I incorporated your ereg into my code however im still getting a PHP error, grateful if you could take a look. The rest of the code has been tested and works fine.


<body>


<form name="search.php" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="id">ID Number</option>
<Option VALUE="surname">Surname</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?

if ($searching =="yes")
{
echo "<h2>Results</h2><p>";


if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}


mysql_connect('localhost', 'un', 'pw') or die(mysql_error());
mysql_select_db("server") or die(mysql_error());


function checkfind($find) {
$find = strtoupper(str_replace(chr(32),'',$find));
if ereg ("^(ABCD[0-9]{2,3}|AB[0-9]{2,3})",$find);

return $find;
else
return FALSE;

}


$find = strip_tags($find);
$find = trim ($find);
$find = str_replace(' ','',$find);


$data = mysql_query("SELECT * FROM officers WHERE upper($field) LIKE'%$find%'");


while($result = mysql_fetch_array( $data ))
{
echo $result['id'];
echo " ";
echo $result['surname'];
echo "<br>";
echo $result['url'];
echo "<br>";
echo $result['expiry'];
echo " ";
echo "<br>";
}


$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}


echo "<b>Searched For:</b> " .$find;
}
?>

</body>