php 带有多个参数的 PHPisset()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14476609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:21:56  来源:igfitidea点击:

PHP isset() with multiple parameters

phpmysqlajax

提问by Bradly Spicer

I'm trying to work with AJAX autocompletes and I am having a few problems with getting the two languages to work in synergy.

我正在尝试使用 AJAX 自动完成功能,但在让两种语言协同工作时遇到了一些问题。

When I replace all the issets with only 1 $_POST the snippet below will work, however by adding another $_POST I get an error on line 5.

当我仅用 1 $_POST 替换所有 isset 时,下面的代码段将起作用,但是通过添加另一个 $_POST 我在第 5 行收到错误。

<?php 

require_once '../Configuration.php';
if (isset($_POST['search_term'] . $_POST['postcode']) == true && empty ($_POST['search_term'] . $_POST['postcode']) == false) {
$search_term = mysql_real_escape_string($_POST['search_term'] . $_POST['postcode']);
$query = mysql_query("SELECT `customer_name`,`postcode` FROM `Customers` WHERE `customer_name` LIKE '$search_term%' ");
while(($row = mysql_fetch_assoc($query)) !== false) {
    //loop
    echo '<li>',$row['customer_name'] . $row['postcode'] '</li>';
}
}


?>

Any advice on why it is throwing this error would be much appreciated. Thanks.

任何关于为什么抛出此错误的建议将不胜感激。谢谢。

I understand I should be using mysqli, I am just trying to get the logic first :)

我知道我应该使用 mysqli,我只是想先了解一下逻辑:)

Js:

JS:

Primary.js:

主要.js:

$(document).ready(function() {
$('.autosuggest').keyup(function() {

    var search_term = $(this).attr('value');
    var postcode = $_GET['postcode'];
    //alert(search_term); takes what is typed in the input and alerts it
    $.post('ajax/search.php', {search_term:search_term, postcode:postcode},     function (data) {
        $('.result').html(data);
        $('.result li').click(function() {
            var result_value = $(this).text();
            $('.autosuggest').attr('value', result_value);
            $('.result').html('');

        });
    });
});
});

回答by Ja?ck

The parameter(s) to isset()must be a variable reference and not an expression (in your case a concatenation); but you can group multiple conditions together like this:

参数 toisset()必须是变量引用而不是表达式(在您的情况下是连接);但是您可以像这样将多个条件组合在一起:

if (isset($_POST['search_term'], $_POST['postcode'])) {
}

This will return trueonly if allarguments to isset()are set and do not contain null.

这将返回true如果只是所有的参数isset()设置,不包含null

Note that isset($var)and isset($var) == truehave the same effect, so the latter is somewhat redundant.

注意isset($var)isset($var) == true具有相同的效果,所以后者有些多余。

Update

更新

The second part of your expression uses empty()like this:

表达式的第二部分使用empty()如下:

empty ($_POST['search_term'] . $_POST['postcode']) == false

This is wrong for the same reasons as above. In fact, you don't need empty()here, because by that time you would have already checked whether the variables are set, so you can shortcut the complete expression like so:

由于与上述相同的原因,这是错误的。事实上,你不需要empty()这里,因为到那时你已经检查了变量是否设置,所以你可以像这样简化完整的表达式:

isset($_POST['search_term'], $_POST['postcode']) && 
    $_POST['search_term'] && 
    $_POST['postcode']

Or using an equivalent expression:

或者使用等效的表达式:

!empty($_POST['search_term']) && !empty($_POST['postcode'])

Final thoughts

最后的想法

You should consider using filterfunctions to manage the inputs:

您应该考虑使用filter函数来管理输入:

$data = filter_input_array(INPUT_POST, array(
    'search_term' => array(
        'filter' => FILTER_UNSAFE_RAW,
        'flags' => FILTER_NULL_ON_FAILURE,
    ),
    'postcode' => array(
        'filter' => FILTER_UNSAFE_RAW,
        'flags' => FILTER_NULL_ON_FAILURE,
    ),
));

if ($data === null || in_array(null, $data, true)) {
    // some fields are missing or their values didn't pass the filter
    die("You did something naughty");
}

// $data['search_term'] and $data['postcode'] contains the fields you want

Btw, you can customize your filters to check for various parts of the submitted values.

顺便说一句,您可以自定义过滤器以检查提交值的各个部分。

回答by Boaz - Reinstate Monica

The parameters of isset()should be separated by a comma sign (,) and not a dot sign (.). Your current code concatenates the variables into a single parameter, instead of passing them as separate parameters.

的参数isset()应该用逗号符号 ( ,) 而不是点符号 ( .)分隔。您当前的代码将变量连接成一个参数,而不是将它们作为单独的参数传递。

So the original code evaluates the variables as a unified stringvalue:

所以原代码将变量求string值为统一值:

isset($_POST['search_term'] . $_POST['postcode']) // Incorrect

While the correct form evaluates them separately as variables:

虽然正确的形式将它们分别评估为变量:

isset($_POST['search_term'], $_POST['postcode']) // Correct

回答by deceze

You just need:

您只需要:

if (!empty($_POST['search_term']) && !empty($_POST['postcode']))

isset && !emptyis redundant.

isset && !empty是多余的。

回答by Gideon

Use the php's OR (||)logical operator for php isset()with multiple operator e.g

将 php 的OR (||)逻辑运算符用于isset()具有多个运算符的 php ,例如

if (isset($_POST['room']) || ($_POST['cottage']) || ($_POST['villa'])) {

}