-1

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!

3
  • What exactly you're trying to do in the second query? Aug 13, 2017 at 9:28
  • From first one i am getting the id-s prntscr.com/g7viv8 of categories and from second one i am getting category names,which are stored in other table in database prntscr.com/g7vjbd Aug 13, 2017 at 9:36
  • 1
    I've given an answer below, hopefully this will resolve your issue. Aug 13, 2017 at 9:47

1 Answer 1

2

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
        }
}
9
  • Thank you,it definitely works,but the result is the same as mine code,it works only when second id is selected ... if(isset($_POST['bulking'])) only return true,which ID is 26 .. second id from categories other two $_POSt variables return false no matter if they are checked or not ;/ Thanks once again,this code is more readable and shorter than mine <3 Aug 13, 2017 at 10:05
  • 1
    @TsvetomirovYordan There are two things here. 1. As per your code, once this if condition if (isset($_POST[$value])) { is met, it'll immediately return true and thereby terminating the while loop there itself, so it won't loop through the rest of the result set once the condition is met. 2. Check whether you're getting all expected contents with $_POST or not, do var_dump($_POST); Aug 13, 2017 at 10:11
  • 1
    @TsvetomirovYordan Paste the contents of var_dump($_POST); on pastebin.com and give me it's link here. May be this clear up the complete scenario. Aug 13, 2017 at 10:33
  • 1
    @TsvetomirovYordan var_dump(...); doesn't show discrete arrays, so that's not a valid output of var_dump($_POST);. Did you edit the contents of var_dump($_POST); before pasting on pastebin? If so then do not edit anything, just paste the exact output of var_dump($_POST); on pastebin.com and give me it's link here. Aug 13, 2017 at 10:45
  • 1
    @TsvetomirovYordan I think I understood the issue. If you could look at the database value and $_POST value, you would see that Fat Loss is stored in the name column but $_POST has Fat_Loss value, see the underscore there. That's why it's not getting matched correctly because Fat Loss != Fat_Loss. I would suggest you to send any other column value(other than name) with your form submission so that it couldn't get changed later. Aug 13, 2017 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.