我想將兩個變量傳遞到ORDER BY $ variable1 $ variable2裏面的MySQL語句。變量從下拉菜單表單中獲得。將變量傳遞給ORDER BY MySQL語句
PHP
if(isset($_POST['order'])){
$sort1 = mysql_real_escape_string($_POST['sort']);
$sort2 = mysql_real_escape_string($_POST['order']);
}
if([email protected]$_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());
?>
HTML表單
<ul class="sort">
<li><form action="topics.php" method="post">
<label class="label">Sort Table By</label>
<select name="sort">
<option value =""> </option>
<option value ="ID">ID</option>
<option value ="Title">Title</option>
<option value ="TAGS">TAGS</option>
<option value ="VIEWS">VIEWS</option>
<option value ="CREATED ON">CREATED ON</option>
<option value ="CREATED BY">CREATED BY</option>
</select>
<label class="label">Order By</label>
<select name = "order" class="tap_Select">
<option value =""> </option>
<option value ="ASC">Ascending</option>
<option value ="DESC">Descending</option>
</select>
<input type="submit" name="order" value="SORT" >
</form>
</li>
</ul>
請幫助。
UPDATE
當我刪除約$ SORT1和$ SORT2
報價我得到這個錯誤: 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
更新2
當我做了$ SQL =「.....」,而不通過mysql_querry() 我得到這個
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
在HTML表單中,您有兩個稱爲'訂單'的元素,select和submit按鈕。 – andrewsi