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:

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
user3478137
  • 9
  • 1
  • 5

5 Answers5

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";
Fisherman
  • 5,949
  • 1
  • 29
  • 35
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.
    }
}
r3wt
  • 4,642
  • 2
  • 33
  • 55
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>
Arsen Ibragimov
  • 425
  • 4
  • 18
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

Community
  • 1
  • 1
KhorneHoly
  • 4,666
  • 6
  • 43
  • 75
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]";
}
}
?>
Strawberry
  • 33,750
  • 13
  • 40
  • 57