简体   繁体   English

邮政编码搜索查询以php格式发送到mysql数据库

[英]Zip code search query in php form to mysql database

I am new to php programming. 我是php编程的新手。 I have set up a mysql database with 我已经设置了一个mysql数据库

id, 
city,
county, 
state,
zip fields

My problem is the search form I have written works well on city and county query's, but will only accept 4 of the five numbers on a query for zip. 我的问题是我编写的搜索表单适用于城市和县的查询,但只接受查询中的5个数字中的4个。 I have researched and found nothing that helps. 我研究过,发现什么都没有帮助。 I thought it may be an issues with preg_replace() , but have had no luck there. 我认为这可能是preg_replace()一个问题,但那里没有运气。

I have tried different mysql query syntax with no luck. 我尝试了不同的mysql查询语法,没有运气。

$allowed = "/([0-9]{5})([0-9]{4})/";
$searchquery = preg_replace($allowed, '', $_POST['searchquery']);
if($_POST['filter1'] == "Zip Code"){
$sqlCommand = "SELECT city, county, zip, state FROM cities_extended WHERE zip LIKE '%$searchquery%' ORDER BY county, city, zip ";

Looks like you're missing a dash in your regex if you're trying to process zip+4. 如果您正在尝试处理zip + 4,看起来您在正则表达式中缺少短划线。 Your code also doesn't quite make sense. 你的代码也没有多大意义。 You aren't trying to replace anything the user puts in, you're trying to find out if what they put in is a valid zip code? 您不是要替换用户放入的任何内容,而是试图找出他们放入的是否是有效的邮政编码? Instead of preg_replace, which replaces a string, try preg_match, which lets you know if they put in a valid zip code. 而不是preg_replace,它取代了一个字符串,尝试preg_match,它可以让你知道他们是否输入了有效的邮政编码。

It should read something like this: 它应该是这样的:

$allowed = "([0-9]{5})-([0-9]{4})";
if($_POST['filter1'] == "Zip Code"){
    if(preg_match($allowed, $_POST['searchquery']))
    {
           $sqlCommand = "SELECT city, county, zip, state FROM cities_extended WHERE zip LIKE '%$searchquery%' ORDER BY county, city, zip ";
    }
} 

Take a look at 看一眼

$string="tampa, florida, 33616-1701";

preg_match("/([0-9]{5})-([0-9]{4})/",$string,$matches);

var_dump( $matches);

or 要么

$allowed = "/([0-9]{5})-([0-9]{4})/";
preg_replace($allowed, $_POST['searchquery'],$matches);
$searchquery=$matches[0];
if($_POST['filter1'] == "Zip Code"){
$sqlCommand = "SELECT city, county, zip, state FROM cities_extended WHERE zip LIKE '%$searchquery%' ORDER BY county, city, zip ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM