PHP and mySQL Dynamic Query -
im trying create dynamic query based upon or selections option values selected user. eg if select project id query return project id, size , lesson stored in second table, or if select size , department query execute. displaying projects of chosen size lessons against it.
heres ive got far. help.
<?php $pid = $_post['project_id'] ; $psize = $_post['projectsize'] ; $pdepts = $_post['depts'] ; $lstage = $_post['stage'] ; $ltype = $_post['type'] ; $impacted = $_post['impacted'] ; $sqlstring = null; echo "total number of captured post variables is:"; echo count($_post); echo '<br />'; $number = 0; foreach ($_post $param_name => $param_val ) { if ($param_val ==""){ }else{ $number++; } echo "param: $param_name = $param_val<br />\n"; } if($number ==1) { }else{ } ?>
i hope can little , added array check , need check security , injection :)
<?php $pid = $_post['project_id'] ; $psize = $_post['projectsize'] ; $pdepts = $_post['depts'] ; $lstage = $_post['stage'] ; $ltype = $_post['type'] ; $impacted = $_post['impacted'] ; //your columns in db $columns = array('project_id'=>'project_id','project_size'=>'project_size','depts'=>'depts','stage'=>'stage'); $sqlstring = null; echo "total number of captured post variables is:"; echo count($_post); echo '<br />'; $number = 0; $querystr = ""; $prestr = array(); foreach ($_post $key => $val ) { if (!empty($_post[$key])){ if(!is_array($_post[$key])) $currentstr = $columns[$key]." = ".$val; else $currentstr = $columns[$key]." in (".implode(',',$_post[$key]).")"; $prestr[] = $currentstr; } } $querystr = "select * tablename ".implode(' , ',$prestr); echo $querystr; if($number ==1) { }else{ } ?>
Comments
Post a Comment