0

I have a problem with the sql picking up the column name as the Value instead of the name itself.

So for example the result returned shows

SELECT ll_project.project_id, ll_project.size, ll_lessons.lesson_title FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id WHERE ll_project.project_id = BSKYB5555
Unknown column 'BSKYB5555' in 'where clause'

From the following Code

$pid = $_POST['project_id'] ;
$psize = $_POST['projectSize'] ;
$pdepts = $_POST['depts'] ;
$lstage = $_POST['stage'] ;
$ltype = $_POST['type'] ;
$impacted = $_POST['impacted'] ;
//Your columns in the 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 as $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 FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id  WHERE ".implode(' AND ',$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 am I doing wrong and why is this picking up the value as a column name?

2 Answers 2

4

Add quotes around your query value

SELECT ll_project.project_id, ll_project.size, ll_lessons.lesson_title FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id WHERE ll_project.project_id = "BSKYB5555"

As there is no quoting, it does not treat it as a string

EDIT

Unfortunately the code and logic is a little hard to follow as there is no commenting

You can try replacing

$currentStr = $columns[$key]." = ".$val; 

with

$currentStr = $columns[$key]." = '".mysql_real_escape_string($val)."'"; 

This should solve your issue and remove the sql injection vulnerability that you currently have by using user input directly in a query.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you show me an example? Not sure where the quotes should go? AHh ok i seee ill try
The SELECT is Generated from the Dynamic Query which is from $columns = array('project_id'=>'ll_project.project_id', etc.... Is it double quotes i need around the 'project_id'?
I am not writing to query initially, It is being grabbed from the Dynamic Query coming from the $columns = array keys and values. I have no way of putting Double Quotes around the "Number" as it is shown when echod from the $queryStr;
Is $val where the BSKYB5555 is coming from?
BSKYB5555 Is coming from $pid = $_POST['project_id'] ; And $columns = array('project_id'=>'ll_project.project_id' Im new to this whole thing so thanks for helping out
|
0

if you use string in query, you have to enclapse it

SELECT ll_project.project_id, ll_project.size, ll_lessons.lesson_title FROM ll_project INNER JOIN ll_lessons ON ll_project.project_id = ll_lessons.project_id WHERE ll_project.project_id = 'BSKYB5555'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.