开发者

Sorting using php, mysql

.Hi guys, i need a little help with a project that i'm currently working on.

I have this database where i have a table containing records of students from different departments namely:

Dept1
Dept2
Dept3
Dept4
Dept开发者_开发问答5 

in my batch.php i have two dropdown menus for Batch(which is the name of the tables) and Department(a field used for sorting).

what i want to do is that when the user chooses a Batch and a Department and then clicks the submit button, it will automatically assign the values chosen to a MySQL query for the SELECT method. the SELECT query should now retrieve all records from the table sort it by department but will display first the Department that was chosen above.

.Any help would be much appreciated. Thanks in advance and more power!


You can sort by a boolean expression, which will return 1 when true and 0 when false.

<?php
    $batch = mysql_real_escape_string($_POST['batch']);
    $department = mysql_real_escape_string($_POST['department']);

    $query = "SELECT * FROM `" . $batch . "`" .
        " WHERE department = '" . $department . "'" .
        " ORDER BY department = '" . $department . "' DESC, department;
?>

Update: You should validate the $batch argument that it is a valid table expected, the code as is is vulnerable to SQL injection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜