Tsvetomirov Yordan Tsvetomirov Yordan -3 years ago 125
PHP Question

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

<?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.

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.

Thank you!

Answer Source

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:

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
        }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download