0

I am trying to Order an SQL select query depending on what the user selects from a drop down list. Here is the code of the list

<select name="order">
  <option value="Patients_name">Name</option>
  <option value="Patients_age">Age</option>
  <option value="Patients_address">Address</option>
</select>

$ord=$_POST['order'];#taking the value from the list
$query="Select * from Patients ORDER BY '$ord'";
$result= mysql_query($query);

If i replace

$query="Select * from Patients ORDER BY '$ord'"; 

by $query="Select * from Patients ORDER BY Patients_age";

Patients_name,Patients_address
It perfectly works though printing $ord would give me the correct value. Any idea why it is not taking the value of $ord

2 Answers 2

1

ORDER BY expects a column name. By putting it in quotes, you're making it a string. Remove the single quotes around $ord.

By the way, what you're doing is incredibly dangerous and open to SQL injection. You should verify the column submitted is in fact a column and allowed.

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

1 Comment

Thank you,i've searched all over and couldn't find anything. And i don't have to worry about security for now.
0

Dont put $org within ''.

$query = "Select * from Patients ORDER BY $ord";

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.