Displaying Dynamic Results PHP mySQL -
i have problem sql picking column name value instead of name itself.
so example result returned shows
select ll_project.project_id, ll_project.size, ll_lessons.lesson_title ll_project inner join ll_lessons on ll_project.project_id = ll_lessons.project_id ll_project.project_id = bskyb5555 unknown column 'bskyb5555' in 'where clause'
from following code
$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'=>'ll_project.project_id','projectsize'=>'ll_project.size','depts'=>'ll_project.deptartment','stage'=>'ll_lessons.stage','type'=>'ll_lessons.type','impacted'=>'impacted'); $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 ll_project.project_id, ll_project.size, ll_lessons.lesson_title ll_project inner join ll_lessons on ll_project.project_id = ll_lessons.project_id ".implode(' , ',$prestr); echo $querystr; echo '<br />'; if($number ==1) { }else{ } $result = mysql_query($querystr) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo ' <tr> <td>'.$row['project_name'].' </td> <td>'.$row['project_id']. ''; }
what doing wrong , why picking value column name?
add quotes around query value
select ll_project.project_id, ll_project.size, ll_lessons.lesson_title ll_project inner join ll_lessons on ll_project.project_id = ll_lessons.project_id ll_project.project_id = "bskyb5555"
as there no quoting, not treat string
edit
unfortunately code , logic little hard follow there no commenting
you can try replacing
$currentstr = $columns[$key]." = ".$val;
with
$currentstr = $columns[$key]." = '".mysql_real_escape_string($val)."'";
this should solve issue , remove sql injection vulnerability have using user input directly in query.
Comments
Post a Comment