I don't have any code for you, but I've done something similar to this recently. Basically I dynamically made the SQL string by concating the string depending on what the user input. Bear with me as I try to type something on the fly.
$columnsArray = array();
$sqlCmd = "INSERT INTO";
If (someCondition) {
array_push($columnsArray, "mplan_date");
}//Could try turning this into a loop and just push all
//applicable columns into the array
$counter = 0;
Foreach ($columnsArray as $columnName) {
$sqlCmd .= " $columnName";
$counter++
If ($counter < count($columnsArray)){
$sqlCmd .= ",";
}//This will put a comma after each column with the
//exception of the last one.
}
So I did something like this only with a user defined select statement to retrieve data. Basiclly keep going with the logic until you’ve built a custom string that will be the user defined SQL statement, and then execute it. This includes doing something similar to the above script to concat the values into the string.
Hope this gives you some ideas.