title: Passing Variables to ORDER BY MySQL Statements
body: <p>I'm trying to pass two variables into ORDER BY $variable1 $variable2 inside MySQL statement. The variables are gotten from a dropdown Menu form.</p>
  
  <h1><strong>PHP</strong></h1>
  
  <pre><code>if(isset($_POST['order'])){
           $sort1 = mysql_real_escape_string($_POST['sort']);
           $sort2 = mysql_real_escape_string($_POST['order']);
          }
          if(!@$_POST['order']){
              $sort1 = 'ID';
              $sort2 = 'DESC';
          }
          $topics = mysql_query(" SELECT topic_id AS 'ID', topic_head AS 'Title', 
                                         topic_tags AS 'TAGS', topic_owner AS 'CREATED BY', topic_date AS 'CREATED ON'
                                  FROM   forum_topics 
                                  ORDER BY '{$sort1}' '{$sort2}'  ") or die (mysql_error());
                          ?&gt;
  </code></pre>
  
  <h1><strong>HTML FORM</strong></h1>
  
  <pre><code>&lt;ul class="sort"&gt;
          &lt;li&gt;&lt;form action="topics.php" method="post"&gt;
                  &lt;label class="label"&gt;Sort Table By&lt;/label&gt;
                  &lt;select name="sort"&gt;
                      &lt;option value =""&gt;    &lt;/option&gt;
                      &lt;option value ="ID"&gt;ID&lt;/option&gt;
                      &lt;option value ="Title"&gt;Title&lt;/option&gt;
                      &lt;option value ="TAGS"&gt;TAGS&lt;/option&gt;
                      &lt;option value ="VIEWS"&gt;VIEWS&lt;/option&gt;
                      &lt;option value ="CREATED ON"&gt;CREATED ON&lt;/option&gt;
                      &lt;option value ="CREATED BY"&gt;CREATED BY&lt;/option&gt;
                  &lt;/select&gt;
  
                  &lt;label class="label"&gt;Order By&lt;/label&gt;
  
                  &lt;select name = "order" class="tap_Select"&gt;
                      &lt;option value =""&gt;    &lt;/option&gt;
                      &lt;option value ="ASC"&gt;Ascending&lt;/option&gt;
                      &lt;option value ="DESC"&gt;Descending&lt;/option&gt;
                  &lt;/select&gt;
  
                  &lt;input type="submit" name="order" value="SORT" &gt;
             &lt;/form&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
  </code></pre>
  
  <p>Please help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When I remove quotes around $sort1 and $sort2</p>
  
  <p>I get this error:
  <code>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SORT' at line 4</code></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>When I did the $sql = "....." , without passing it through mysql_querry()
  I got this</p>
  
  <p><code>SELECT topic_id AS 'ID', topic_head AS 'Title', topic_tags AS 'TAGS', topic_owner AS 'CREATED BY', topic_date AS 'CREATED ON' FROM forum_topics ORDER BY ID SORT</code></p>