0

I have multiple checkboxes in my websites: checkbox 1 checkbox 2 checkbox 3 etc.

I want to dynamically generate mysql query based on the above checkboxes.

i:e if 1st checkbox is selected then the query should be:

$mysql="Select * from mytable where colname=" . $checkbox1 .;

if 1st and 2nd checkbox is selected then the query should be:

$mysql="Select * from mytable where colname=" . $checkbox1 ."AND colname=" . $checkbox2 ." ;

if all are selected then it should be :

$mysql="Select * from mytable where colname=" . $checkbox1 . "AND colname=" . $checkbox2 ."AND colname=" . $checkbox3 .  ;

Can someone pls help:

1
  • 2
    you've pretty much got it all figured out. now you just have to write it! Commented Apr 10, 2014 at 10:58

5 Answers 5

1

you have to change your form like follow because its taking multiple value its should be post as an array

<form action="register.php" method="POST"> 
  <input type="checkbox" name="rating[]" value="5">5 Star 
  <input type="checkbox" name="rating[]" value="4">4 Star 
  <input type="checkbox" name="rating[]" value="3">3 Star 
  <input type="checkbox" name="rating[]" value="2">2 Star 
  <input type="checkbox" name="rating[]" value="1">Less than 2 Star 
</form>

Then in php

  $where = '';
   if(isset($_POST['rating'])){
     $data = implode(',',$_POST['rating']); // beacuse your rating is only one column in db i think
     $where = "WHERE cloumn_name IN($data)";
   }
  $query = "SELECT * FROM your_table $where";
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but my questions is for the case when multiple checkboxes are selected.
can you give your html for checkbox ?
Here it is: <form action="register.php" method="POST"> <input type="checkbox" name="rating" value="5">5 Star <input type="checkbox" name="rating" value="4">4 Star <input type="checkbox" name="rating" value="3">3 Star <input type="checkbox" name="rating" value="2">2 Star <input type="checkbox" name="rating" value="1">Less than 2 Star </form>
1

You can use string concatenation with a bit of trickery to get the job done. do not rely on isset, instead use !empty.

<form action="example.php" method="post">
    <input type="checkbox" name="Col1" value="colname" />Col 1</br>
    <input type="checkbox" name="Col2" value="colname" />Col 2</br>
    <input type="checkbox" name="Col3" value="colname" />Col 3</br>
</form>
<?php
if(!empty($_POST)) {
    $string = '';
    if(!empty($_POST['col1'])) {

        $string.= "column1 ='".$_POST['col1']."'";
    }   
    if(!empty($_POST['col2'])){

        if(!empty($string)) {

            $string.=" AND ";

        } 
        $string.= "column2 ='".$_POST['col2']."'";

    }
    if(!empty($_POST['col3'])){
        if(!empty($string)) {

            $string.=" AND ";

        } 

        $string .= "column3 ='".$_POST['col3']."'";
    }

    if(!empty($string)) 
    {
        //execute your query here.
    }
}

Comments

0

Something about it

<? if ($_POST[option]==1) { 
      //query 
   } 
?>

<form method="post">
   <input type="checkbox" name="option" value="1">1
   <input type="checkbox" name="option" value="2">2
   <input type="checkbox" name="option" value="3">3
</form>

1 Comment

but what when multiple checkboxes are selected.for example there are 10 checkboxes: 1,2,3....10.
0

Just check if your Checkboxes are selcted via (isset($_POST[checkbox1]))

So you could do something like

if (isset($_POST[checkbox1])
{
//statement
}
else if (isset($_POST[checkbox2])
{
//statement2...
}
...

Read more here

Cheers

Comments

0

It should be apparent from the code provided below that I'm certainly no PHP coder. But anyway, here's an idea to think about...

Try it with different values for input between 0 and 127, e.g. ?input=66

<?php

if(isset($_GET['input'])){

   $input = $_GET['input'];

   }else{

   $input=0;
}

$days = array('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun');

for( $i=0; $i<7; $i++ ) {
    $daybit = pow(2,$i);
    if( $input & $daybit ) {
        echo "<input type = checkbox checked/>$days[$i]";
    }else{
        echo "<input type = checkbox>$days[$i]";
}
}
?>

Comments

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.