duanfan8360 2019-03-04 12:31
浏览 456

sql语句中的变量

I have the following code

if(isset($_POST['submit']) || isset($_POST['mon']) || isset($_POST['yer']) || 
    isset($_POST['acty'])) {
        $mon = $_POST['mon'];
        $yer = $_POST['yer'];
        $acty = $_POST['acty'];
}

$str = "SELECT pty, SUM(`PW`) as Total 
        FROM heal 
        WHERE mon='$mon' 
          AND yer='$yer'  
        GROUP BY pty";

how can i pass the variable $acty into this: SUM('PW')....That is SUM('$acty')

  • 写回答

1条回答 默认 最新

  • dongzha0149 2019-03-04 12:37
    关注

    exactly as you said.

    $str = "
        SELECT 
            pty, 
            SUM($acty) as Total 
        FROM 
            heal 
        WHERE 
            mon='$mon' AND 
            yer='$yer'  
        GROUP BY 
            pty";
    

    You can have variables inside double quotes and php will give you its contents.

    Side Note: Your code is open for SQL Injection, you must need to prevent your code with SQL injection. Some useful links:

    How can I prevent SQL injection in PHP?

    Are PDO prepared statements sufficient to prevent SQL injection?

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?