简体   繁体   English

如何检查是否用foreach循环设置了所有$ _POST变量

[英]How to check if all $_POST variables are set with foreach loop

I want to check if given $_post variable is set and after that execute a query(which is another theme).But i have a big trouble for 5 hours i cannot really pass it.. There is my function code 我想检查是否设置了给定的$ _post变量,然后执行查询(这是另一个主题)。但是我有5个小时的麻烦,我无法真正通过它。

<?php
function post_category_isset()
{
    global $connect;

    $input = sanitize('category');
    $query = mysqli_query($connect, "SELECT term_id FROM term_taxonomy WHERE taxonomy = '$input'");

    while ($row = mysqli_fetch_assoc($query)) {

        $category_id [] = $row['term_id'];
        $result = '\'' . implode('\',\'', $category_id) . '\'';

        foreach ($row as $result) {

            $query1 = mysqli_query($connect, "SELECT name FROM terms WHERE term_id = $result");
            $fetch = mysqli_fetch_assoc($query1);
            $value = $fetch['name'];
            if (isset($_POST[$value])) {
                echo "if(isset($_POST[$value]) OR";

                return true;
            }
        }
    }
}


?>

I have tried everything,and yes the code looks really stupid the way it is right now,but i am depleted ... 我已经尝试了一切,是的,代码看起来确实很愚蠢,但是我已经耗尽了...

So i made that function to check if categories variables are set,which are another function,because i want everything to be automatically .I will paste my other func in pastebin,since it is out of the thread here . 所以我做了功能检查类别变量设置,这是另一种功能,因为我想要的一切是自动。我会贴上我的其他FUNC在引擎收录,因为它是线程的出在这里

So all in short i want to make function which checks if all $_POST variables from previous functions are set and if they are,fire the query. 所以总之我想做一个函数来检查以前函数中的所有$ _POST变量是否都已设置,如果是,则触发查询。

Thank you! 谢谢!

You don't have to use two separate queries here, you can do everything in just one single query. 您不必在这里使用两个单独的查询,您只需一个查询即可完成所有操作。

SELECT name 
FROM terms 
WHERE term_id IN (
    SELECT term_id 
    FROM term_taxonomy 
    WHERE taxonomy = '$input'
)

So your post_category_isset() function would be like this: 因此,您的post_category_isset()函数将如下所示:

function post_category_isset(){
    global $connect;

    $input = sanitize('category');
    $query = mysqli_query($connect, "SELECT name FROM terms WHERE term_id IN (SELECT term_id FROM term_taxonomy WHERE taxonomy = '$input')");
    while ($row = mysqli_fetch_assoc($query)) {
        $value = $row['name'];
        if (isset($_POST[$value])) {
            // your code
        }
}

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

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