Hello,
I am trying to validate a variable using a regex and preg_match. I want it to do a query if the preg_match passes. Trying to allow letters, spaces, hyphens, and apostrophes (single quotes). Not sure what my problem is as I am a bit of a noob with regex. My code is as follows any help is appreciated or advice if there is a better way to accomplish this. Thanks:
PHP Code:$lastname = $_GET['last'];
if(preg_match("/^[A-Za-z\\- \']+$/", $lastname)){
$query = "SELECT * FROM main WHERE last LIKE '$lastname%' ORDER BY last, first, middle ASC";
if ($result = $mysqli->query($query)):
$row_cnt = mysqli_num_rows($result);
if($row_cnt > 0){
echo '<table border="1">';
echo '<tr><th>Last Name</th><th>First Name</th><th>Middle Name</th><th>Date of Birth</th><th>Street</th><th>City</th></tr>';
while ($obj = $result->fetch_object()) {
printf ('<tr><td>'."%s".'</td>', $obj->last);
printf ('<td>'."%s".'</td>', $obj->first);
printf ('<td>'."%s".'</td>', $obj->middle);
printf ('<td>'."%s".'</td>', $obj->dob);
printf ('<td>'."%s".'</td>', $obj->street);
printf ('<td>'."%s".'</td></tr>', $obj->city);
}
echo '</table>';



Reply With Quote
Bookmarks