SQL statements created 'on the fly' with PHP.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

SQL statements created 'on the fly' with PHP.

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

cheers will look into this.
Post Reply