0

我正在尝试根据提交的表单的内容定义一个变量,并针对 MySQL 数据库进行检查,但 preg_match 和 $variable 匹配不会一起工作。他们独立工作,但不一起工作。

我在每一步都打印出每个变量,并证明提交、比较和检索的数据始终存在,但是在比较 ifelse 语句中的两个变量时我无法定义变量: elseif ( !preg_match("/bt/i",$zip ) 或 $country !== '爱尔兰' )

过程是:表单提交->比较变量与数据库->输出变量取决于数据库比较。

示例表单提交(示例):

联邦调查局 (12345678901)

城市(贝尔法斯特)

国家(英国)

邮编(BT1 1DS)

这是导致问题的(减少的)代码:

    $country = $location['country']; //good value: Ireland / bad value: Italy(or empty)
    $zip = $location['zip']; //good value: BT1 1DS / bad value: 00(or empty)

    if ($fbid == $id) { //this works
    $confirmpage = '<h3>Sorry, this page is already in the table.</h3>';
    } elseif ( !preg_match("/bt/i",$zip) or $country !== 'Ireland' ) { //confirm location
    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
    } else { //success, page can be submitted
    $confirmpage = '<h3>Confirm Page</h3>';
    }

代码不起作用是:

        elseif ( !preg_match("/bt/i",$zip) or $country !== 'Ireland' )

当我删除此语句时,脚本的其余部分工作正常。如果选项 1 是否定的,在此声明到位的情况下,结果始终为选项 2,即使提交的表格在 zip 开头包含 BT 或将爱尔兰作为国家/地区:

    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
4

3 回答 3

0

我创建了一个基本的 php 页面代码并复制到您的代码中 - 然后手动将 $country 和 $zip 设置为好或坏的值 - 它按预期工作,因此表明您的 preg_match 没有问题。

于 2012-07-07T17:30:30.167 回答
0

通过更改检查的执行方式对其进行排序。而不是:如果“条件”或“条件2”,我使用 ifelse 来检查正数:

    $confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
    if ($fbid == $id) {
$confirmpage = '<h3>Sorry, this page is already in the table.</h3>';
} elseif ( preg_match("/bt/i",$zip) ) { //BT post code present
$confirmpage = $confirm;
} elseif ($country == 'Ireland') { // //Ireland present
$confirmpage = $confirm;
} else { //confirm page
$confirmpage = '<h3>This page is not in Northern or Southern Ireland</h3>';
于 2012-07-12T13:50:56.797 回答
0

尝试

elseif ( preg_match("/bt/i",$zip) == 1 || $country != 'Ireland' )
于 2012-07-07T17:24:45.723 回答