Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

dynamic mysql SELECT qeury using checkboxes

11 like 0 dislike
what I am trying to do is build a query using checkboxes
The table will stay the same its the columns that the checkboxes will refer to.

SELECT thisField, anotherField FROM members

thisField of course would be one of the checkboxes, I guess it would have to check if box is checked then build the query from there

any help would be greatly appreciated , this is killing me
asked 2 years ago by webmaster (25,380 points)

1 Answer

1 like 0 dislike
 
Best answer
Does any of this code make sense ?
I don't want to explain anything unless I need to.

php Code:

<?php

$expected_fields = array(
  'one' => true,
  'two' => true
);

$sql_fields = '';

foreach((array)@$_POST['fields'] as $key => $val)
{
  if( ! empty($expected_fields[$key]))
  {
    $sql_fields .= ",$key";
  }
}

if(strlen($sql_fields) > 0)
{
  $sql = 'SELECT ' . substr($sql_fields, 1) . ' FROM ...';
  // run query
}

?>


Code:

<input type="checkbox" name="fields[one]" value="1"/>
<input type="checkbox" name="fields[two]" value="2"/>
answered 2 years ago by monirulislam (51,620 points)

Related questions

9 like 0 dislike
1 answer
6 like 0 dislike
1 answer
6 like 0 dislike
1 answer
14 like 0 dislike
1 answer
asked 2 years ago by biswaskeran (70,430 points)
3 like 0 dislike
2 answers