0

I have stored multiple user`s information in a table in mysql. These are supposed to be categorized under 5 tags, A, B, C, D and E. The information is stored in the table as follows:

  1. maybe all the above mentioned tags are used
  2. maybe 'A' is used multiple times, same as B, C, etc.
  3. maybe only a few tags are used and other are never used to categorize

I am providing checkbox options for these tags in my html generated by a php script, and when the user checks some or all of the options, it should retrieve the checked tags information from the table and do something.

But in my case, when some tags are checked and those are not stored in the table, I am getting an error (since their is no tag information in the table, obviously). In that case, how should I program, so that even though I checked some tags, and their information is not in the mysql table, the program should ignore them and display the information related to other checked tags. I am not able to write the program in PHP with MySQL queries. It can be a checkbox or a select.

Let me know if the question is unclear, since it is a bit complex to explain here in words.

7
  • 2
    It is not very clear indeed. May I suggest you show us some (mock) data by drawing a table (hint, wrap it in <pre> tags). Describe the result you are getting and the result you are expecting. Show us some code, a minimal example stackoverflow.com/help/mcve And what do you mean when you say you are not able to write in PHP or MySQL? Commented May 24, 2016 at 21:00
  • 1
    We can't help you blindly. You need to show us some code and better explanation because your question is not understandable Commented May 24, 2016 at 21:15
  • Well I dont get if fully but why dont you just show the checkboxes that are relevant and stored in your table? Commented May 24, 2016 at 22:26
  • @Pevara : I was working on the problem, and all I want is: How to pass multiple values from checkboxes into the mysql query but giving 'OR' condition. My query on the terminal goes like: "select id from tablename where tag= 'A' or 'B' or 'C';" I want to implement this query in the database, where the values given through the OR condition are those selected in the checkboxes each time dynamically. Commented May 26, 2016 at 21:51
  • @A.Ilazi : please look my above comment Commented May 26, 2016 at 21:56

1 Answer 1

1

To what I finally understand, you want to query your database according to the option the user selected through a checkbox. But if the user can't select more than one option then I suggest you use the radio button giving all radio button the same name so the user can select only one option and run your code like this:

<form action="page.php" method="post">
    <input type="radio" name="tag" value="A" />
    <input type="radio" name="tag" value="B" />
    <input type="radio" name="tag" value="C" />
    <input type="submit" name="submit" />
</form>

Then your php page should be like this:

<?php

    //your db connection here

    if(isset($_POST['submit'])){
        $tag = $_POST['tag'];

        // remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to your db connection
        $query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$tag."'");

    }   

?>

But if the user can select more than one option then you can use the checkbox and run your code like this:

<form action="page.php" method="post">
    <input type="checkbox" name="a" />
    <input type="checkbox" name="b" />
    <input type="checkbox" name="c" />
    <input type="submit" name="submit" />
</form>

And this should be your php page:

<?php

    //your db connection here

    if(isset($_POST['submit'])){
        $tag1 = $_POST['a'];
        $tag2 = $_POST['b'];
        $tag3 = $_POST['c'];

        //here you assign variables to each checkbox if they are checked
        //but if they aren't you assign the variable to be null or anything
        //you wish as long as it won't tamper with your query

        if($tag1 == "on"){
            $a = "A";
        } else {
            $a = ""; //assign any wrong value here
        }
        if($tag2 == "on"){
            $b = "B";
        } else {
            $b = ""; //assign any wrong value here
        }
        if($tag3 == "on"){
            $c = "C";
        } else {
            $c = ""; //assign any wrong value here
        }

        // remember you should use mysqli because mysql is deprecated and $c0n will be the variable assigned to you db connection
        $query = mysqli_query($con, "SELECT id FROM table WHERE tag='".$a."' OR tag='".$c."' OR tag='".$c."'");

    }   

?>

Hope this will help you

Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome! standard copy/paste errors, I know them well ;-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.