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.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大