PHP help with building query

I have several drop lists where if no option is selected then the value is = ""...

I cant figure out how to build the query for mysql in PHP.

query = SELECT * FROM db

13.10.2009 15:28:33
You mean dropdown lists? And you want to build the SELECT query based on the item selected on that lists?
Lukasz Lysik 13.10.2009 15:31:21
4 ОТВЕТА

Too less information, but here's what I would do

$rows = $db->query(
  'select * 
   from 
     table 
   where 
      checkbox_value = ?',
  $_POST['checkbox']
);

In $rows you will have all the data you need.

0
13.10.2009 15:32:10
I would do similar. However, you should consider what to do when the selected value is empty (like in the question).
Lukasz Lysik 13.10.2009 15:33:48

You can run a SELECT on a table not on a DB! A Database consists of many tables. See http://www.php.net/manual/en/function.mysql-select-db.php

0
13.10.2009 15:34:20

Check out w3Schools sql tutorials.

Or more specifically the select tutorial

Also the PHP/mysql tutorial will give you all that you need for this stuff.

0
13.10.2009 15:37:55

I assume you have a select like this:

<select name="data[]" multiple="multiple">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>

Your php can be something like

<?php
$data = array();
$data = $_POST['data'];
$query = "select * from table";
if (count($data > 0)) {
    for ($i = 0; $i < count($data); $i++) {
        $data[$i] = "'{$data[$i]}'";
    }
    $query .= " where field in (".implode(",", $data).")";
}
1
13.10.2009 15:45:23