duanmeng1950 2016-02-28 18:07
浏览 229

如何使用多个SUM()创建mySQL语句?

My site adds product identifiers to a session variable (an array called "itemarray") when a button is clicked on the product page. When the user opens their shopping cart, the following code executes:

<?php
    $cart = implode(',', $_SESSION['itemarray']);
    /*Create connection to DB here. DB connection is called $connection.*/
    $result = $connection->query("SELECT SUM(Price) AS Total_Price, SUM(Tax) AS Total_Tax, SUM(Shipping) AS Total_Shipping FROM STOCK_LIST WHERE Product_ID IN ($cart)");
    while ($rows = $result->fetch_assoc()) {
        /*Output query result into table*/
    }
    /*Terminate the connection to the database*/
    $connection->close();
?>

The first line of code goes through the itemarray and appends each element into a comma-delimited list (called $cart). A connection is made to the database (the code for which has been removed because it has already been tested), and a query is submitted which asks for the total price, tax, and shipping costs of the items in $cart. A while loop is then run which echos the contents of the $results variable into a table (the code for which has been removed because it has already been tested).

The issue is that the calculated totals aren't being output. The implode function works as expected, the connection is being made, and the table is being created (just without the output from the query), so I can only assume that the query is the problem.

  • 写回答

2条回答 默认 最新

  • dsf8897 2016-02-28 18:25
    关注

    I would strongly recommend you using binding parameters as it will save your site from SQL injection attack (which I assume would be crucial for your shop site). Try to adopt the solution from this question.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试