如何从表中获取具有字段和许多输入值的行来检查字段?

时间:2022-02-23 12:57:48

I have a table name wp_postmeta and some fields one of them is meta_value. I want to input 3 values and check against meta_value field.

我有一个表名wp_postmeta,其中一些字段是meta_value。我想输入3个值并检查meta_value字段。

$price = $_POST['prices'];
$city = $_POST['city'];
$hotels= $_POST['hotels'];

And my query is like

我的查询就像

SELECT post_id,meta_key,meta_value
    FROM `wp_postmeta`
    WHERE (`meta_value` LIKE '%$price%'  
    OR `meta_value` LIKE '%$hotels%'
    OR `meta_value` LIKE '%$city%')
   AND (meta_value IS NOT NULL)

Here if i give 3 inputs then it will be ok, if i left one field empty to compare 2 input values against meta_value then it will list all rows

Any idea?? please share.

这里如果我给3个输入那么它就没问题,如果我将一个字段留空以比较2个输入值与meta_value然后它将列出所有行任何想法?请分享。

3 个解决方案

#1


1  

Try,

尝试,

$query = "SELECT post_id,meta_key,meta_value
    FROM `wp_postmeta` ";

$where = '';

if(trim($price) != '') {
$where .= "`meta_value` LIKE '%$price%'  ";
}

if(trim($hotels) != '') {
if($where != '') $where .= " OR ";
$where .= " `meta_value` LIKE '%$hotels%' ";
}

if(trim($city) != '') {
if($where != '') $where .= " OR ";
$where .= "`meta_value` LIKE '%$city%'";
}

if(trim($where) != '') {
$where = " WHERE ($where) AND (meta_value IS NOT NULL)";
} else {
$where = " WHERE (meta_value IS NOT NULL)";
}

$query = $query.$where;

And, try using $query in place of actual query in php.

并且,尝试使用$ query代替php中的实际查询。

#2


0  

To compare against less than all three, completely remove the portion of the where clause applicable to the variable you're not comparing:

要与少于全部三个进行比较,请完全删除适用于您未比较的变量的where子句部分:

E.G. to not compare $city:

例如。不比较$ city:

SELECT post_id,meta_key,meta_value
    FROM `wp_postmeta`
    WHERE (`meta_value` LIKE '%$price%'  
    OR `meta_value` LIKE '%$hotels%')
   AND (meta_value IS NOT NULL)

#3


0  

One way to do this is with your variable assignment:

一种方法是使用变量赋值:

$price = $_POST['prices'];
$city = $_POST['city'];
$hotels= $_POST['hotels'];

Change to:

改成:

$price = (isset($_POST['prices']) && trim($_POST['prices']) != '' ? filter_var($_POST['prices'],FILTER_VALIDATE_STRING) : null);

I used null, but you could use another value that would guarantee a non-match.

我使用null,但你可以使用另一个值来保证不匹配。

Also, as mentioned in other comments, be sure to use prepared statements.

另外,如其他评论中所述,请务必使用预准备语句。

#1


1  

Try,

尝试,

$query = "SELECT post_id,meta_key,meta_value
    FROM `wp_postmeta` ";

$where = '';

if(trim($price) != '') {
$where .= "`meta_value` LIKE '%$price%'  ";
}

if(trim($hotels) != '') {
if($where != '') $where .= " OR ";
$where .= " `meta_value` LIKE '%$hotels%' ";
}

if(trim($city) != '') {
if($where != '') $where .= " OR ";
$where .= "`meta_value` LIKE '%$city%'";
}

if(trim($where) != '') {
$where = " WHERE ($where) AND (meta_value IS NOT NULL)";
} else {
$where = " WHERE (meta_value IS NOT NULL)";
}

$query = $query.$where;

And, try using $query in place of actual query in php.

并且,尝试使用$ query代替php中的实际查询。

#2


0  

To compare against less than all three, completely remove the portion of the where clause applicable to the variable you're not comparing:

要与少于全部三个进行比较,请完全删除适用于您未比较的变量的where子句部分:

E.G. to not compare $city:

例如。不比较$ city:

SELECT post_id,meta_key,meta_value
    FROM `wp_postmeta`
    WHERE (`meta_value` LIKE '%$price%'  
    OR `meta_value` LIKE '%$hotels%')
   AND (meta_value IS NOT NULL)

#3


0  

One way to do this is with your variable assignment:

一种方法是使用变量赋值:

$price = $_POST['prices'];
$city = $_POST['city'];
$hotels= $_POST['hotels'];

Change to:

改成:

$price = (isset($_POST['prices']) && trim($_POST['prices']) != '' ? filter_var($_POST['prices'],FILTER_VALIDATE_STRING) : null);

I used null, but you could use another value that would guarantee a non-match.

我使用null,但你可以使用另一个值来保证不匹配。

Also, as mentioned in other comments, be sure to use prepared statements.

另外,如其他评论中所述,请务必使用预准备语句。