dqce48404 2015-07-28 16:40
浏览 24
已采纳

使SQL识别来自PHP / html选择选项的变量

I think the answer to this may be simple, but being new to SQL I am still growing. Here's my dilemma. I have a php array of options with 10 values. When any one option is selected it is passed into a variable named "spots". I have 10 SQL SELECT statements that pull 1 of 10 different tables. The issue is that I do not know exactly what to do in order to get the SQL to recognize which value was selected and based on which was selected show that specific table data. (This would be easy if I were able to use the JavaScript Switch statement, but I do not know an equivalent for that)

EXAMPLE:

PHP

$spots = ["Report1","Report2","Report3","Report4","Report5","Report6","Report7","Report8","Report9","Report10"];

SQL

SELECT *, FROM Report5
ORDER BY TW ASC;

Now how do I get SQL to loop through an array to find a match, then depending on that match select from a list of commands (for example like a JavaScript switch statement)?

  • 写回答

1条回答 默认 最新

  • douleijiang8111 2015-07-28 16:44
    关注

    Use variable substitution:

    foreach ($spots as $spot) {
        $sql = "SELECT * FROM $spot ORDER BY TW ASC";
        // perform the SQL query using $sql, do what you want with the results
    }
    

    Make sure you've validated that the values in $spots are valid if they're coming from the user. Otherwise you'll be subjecting your code to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?