Page 1 of 1

SQL statements created 'on the fly' with PHP.

Posted: Thu May 27, 2004 6:15 am
by hairyjim
Hi,

Basically I want to execute a dynamically created SQL statement based on user input.

Here is my thought.

Two select boxes one called 'Product' the other called 'Category'.

Now initially when the page first loads it will show all technical articles, and then if the user wishes they can filter the list using the boxes available. i.e. Product AND Category, just Product, just Category.

The problem I have here I have no idea how to create an SQL select based on user input that is posted to the same php page.

Any indicators as to where I can info on doing this will be greatly appreciated.

Posted: Thu May 27, 2004 7:07 am
by CoderGoblin
Use something like....

Code: Select all

// POTENTIAL SECURITY NOTICE : need to check here for validity
$variable1=floor($_POST['cat']);

// Calculate filter
if ($variable1) {
   $sql_add=" AND columnx=$variable1";
} else {
  $sql_add="";
}

$query=" SELECT * from table1 WHERE test='i dont know".$sql_add;

// Perform the query
Hope this helps

Posted: Thu May 27, 2004 7:10 am
by hairyjim
cheers will look into this.