user3478137
user3478137

Reputation: 9

Dynamically Creating mysql select Query

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:

Upvotes: 0

Views: 636

Answers (5)

Strawberry
Strawberry

Reputation: 33935

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]";
}
}
?>

Upvotes: 0

Fisherman
Fisherman

Reputation: 6121

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";

Upvotes: 1

r3wt
r3wt

Reputation: 4732

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

Upvotes: 1

KhorneHoly
KhorneHoly

Reputation: 4766

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

Upvotes: 0

Arsen Ibragimov
Arsen Ibragimov

Reputation: 435

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>

Upvotes: 0

Related Questions