Search code examples
javascriptphpmysqlprocedures

SQL Update Column on new Datarow


I've a MySQL server and I'm running a WebShop on it. Is it possible to calculate the sum of all articles on an order. Now when I add a new row I have to call a method in my PHP Script to calculate the sum. Is this possible to automate in MySQL (ex. Procedures)?

With Best Regards Kevin Horvat


Solution

  • $query="SELECT SUM(tbl_articles.price) as sum FROM ztbl_articles_orders INNER JOIN tbl_articles ON ztbl_articles_orders.articleID = tbl_articles.articleID INNER JOIN tbl_orders ON ztbl_articles_orders.orderID = tbl_orders.orderID WHERE tbl_orders.userID = 3;";
    $result= mysqli_query($connection,$query);
    $row=mysqli_fetch_array($result,MYSQLI_BOTH);
    $sum=$row['sum'];
    

    "$sum" gets the value you want, now you can insert or update with this value.