• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Sum Column in MySQL using PHP/MySQLi

Sneus

Attack Surface Analyzer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
175
Likes
147
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
In this tutorial, I'm going to show you how to sum MySQL columns using PHP/MySQLi. I've also included in this tutorial the use of GROUP BY in MySQLi query and the 2 MySQLi methods which I have included in the comments. This tutorial does not include a good design but will give you an idea of the topic.

Getting Started

  1. Download and Install XAMPP or any equivalent to run PHP script.
  2. Open the XAMPP's Control Panel and start the "Apache" and "MySQL".
  3. Prepare a text editor such as notepad++ and sublime text editor for the coding stages.
  4. Create a new folder in your XAMPP's "htdocs" folder for this tutorial.

Creating our Database

First, we're going to create our database.

  1. Open PHPMyAdmin

    in a browser. i.e. http://localhost/phpmyadmin
  2. Click databases, create a database and name it as "sum

    "
    .
  3. After creating a database, click the SQL and paste the below codes. See the image below for detailed instructions.

  1. CREATE

    TABLE

    `product`

    (
  2. `productid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `product_

    name`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. PRIMARY KEY

    (

    `productid`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  1. CREATE

    TABLE

    `sales`

    (
  2. `salesid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `productid`

    int

    (

    11

    )

    NOT

    NULL

    ,
  4. `sales_

    qty`

    double

    NOT

    NULL

    ,
  5. PRIMARY KEY

    (

    `salesid`

    )
  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

database_6_5.png


Inserting Data into our Database

Next is to insert sample data into our database. I this tutorial, we're gonna insert sample products.

  1. Click the "sum" database that we have created.
  2. Click the SQL tab and paste the code below.

  1. INSERT

    INTO

    `product`

    (

    `product_

    name`

    )

    VALUES


  2. (

    'Apple'

    )

    ,
  3. (

    'Orange'

    )

    ,
  4. (

    'Strawberry'

    )

    ;

Creating our Connection

The next step is to create a database connection and save it as "conn.php

"
. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php

  2. //MySQLi Procedural
  3. //$conn = mysqli_connect("localhost","root","","sum");
  4. //if (!$conn) {
  5. // die("Connection failed: " . mysqli_connect_error());
  6. //}

  7. //MySQLi OOP
  8. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "sum"

    )

    ;
  9. if

    (

    $conn

    ->

    connect_error

    )

    {
  10. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  11. }

  12. ?>

Creating Our Table and our Form

Next is to create our table and our add form. In this case, we will create a sample sales table and add a sale form. We name this "index.php

"
.

  1. <?php

    include

    (

    'conn.php'

    )

    ;

    session_start

    (

    )

    ;

    ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>How to Sum Column in MySQL using PHP/MySQLi</title>
  6. </head>
  7. <body>
  8. <h3>Sales Table</h3>
  9. <table border="1">
  10. <th>Product Name</th>
  11. <th>Quantity</th>
  12. <?php
  13. $total_qty

    =

    0

    ;

  14. //MySQLi Procedural
  15. //$query=mysqli_query($conn,"select * from sales left join product on product.productid=sales.productid order by product.product_name asc");
  16. //while($row=mysqli_fetch_array($query)){
  17. /* ?>
  18. <tr>
  19. <td><?php echo $row['product_name']; ?></td>
  20. <td><?php echo $row['sales_qty']; ?></td>
  21. </tr>

  22. <?php */
  23. // $total_qty += $row['sales_qty'];
  24. //}

  25. //MySQLi OOP
  26. $query

    =

    $conn

    ->

    query

    (

    "select * from sales left join product on product.productid=sales.productid order by product.product_name asc"

    )

    ;
  27. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  28. ?>
  29. <tr>
  30. <td><?php

    echo

    $row

    [

    'product_name'

    ]

    ;

    ?>

    </td>
  31. <td><?php

    echo

    $row

    [

    'sales_qty'

    ]

    ;

    ?>

    </td>
  32. </tr>
  33. <?php
  34. $total_qty

    +=

    $row

    [

    'sales_qty'

    ]

    ;
  35. }
  36. ?>
  37. <tr>
  38. <td>TOTAL QTY:</td>
  39. <td><?php

    echo

    $total_qty

    ;

    ?>

    </td>

  40. </tr>
  41. </table>
  42. <div style="position:relative; left: 300px;">
  43. <h3>Group By Product</h3>
  44. <ul>
  45. <?php
  46. //MySQLi Procedural
  47. //$a=mysqli_query($conn,"select *, sum(sales_qty) as total_sales from sales left join product on product.productid=sales.productid group by sales.productid");
  48. //while($arow=mysqli_fetch_array($a)){
  49. /* ?>
  50. <li>Total <?php echo $arow['product_name'] ?>: <?php echo $arow['total_sales']; ?></li>
  51. <?php */
  52. //}

  53. //MySQLi OOP
  54. $a

    =

    $conn

    ->

    query

    (

    "select *, sum(sales_qty) as total_sales from sales left join product on product.productid=sales.productid group by sales.productid"

    )

    ;
  55. while

    (

    $arow

    =

    $a

    ->

    fetch_array

    (

    )

    )

    {
  56. ?>
  57. <li>Total <?php

    echo

    $arow

    [

    'product_name'

    ]

    ?>

    : <?php

    echo

    $arow

    [

    'total_sales'

    ]

    ;

    ?>

    </li>
  58. <?php
  59. }
  60. ?>

  61. </ul>
  62. <h3>Insert New Sales</h3>
  63. <form method="POST" action="add_sale.php">
  64. <select name="sales_product">
  65. <option value="0">Select Product</option>
  66. <?php
  67. $p

    =

    $conn

    ->

    query

    (

    "select * from product"

    )

    ;
  68. while

    (

    $prow

    =

    $p

    ->

    fetch_array

    (

    )

    )

    {
  69. ?>
  70. <option value="<?php

    echo

    $prow

    [

    'productid'

    ]

    ;

    ?>

    "><?php

    echo

    $prow

    [

    'product_name'

    ]

    ;

    ?>

    </option>
  71. <?php
  72. }

  73. //$p=mysqli_query($conn,"select * from product");
  74. //while($prow=mysqli_fetch_array($p)){
  75. /* ?>
  76. <option value="<?php echo $prow['productid']; ?>"><?php echo $prow['product_name']; ?></option>
  77. <?php */
  78. //}
  79. ?>
  80. </select>
  81. Qty: <input type="text" name="sales_qty" required>
  82. <input type="submit" value="ADD">
  83. </form>
  84. <span>
  85. <?php
  86. if

    (

    isset

    (

    $_SESSION

    [

    'msg'

    ]

    )

    )

    {
  87. echo

    $_SESSION

    [

    'msg'

    ]

    ;
  88. unset

    (

    $_SESSION

    [

    'msg'

    ]

    )

    ;
  89. }
  90. ?>
  91. </span>
  92. </div>
  93. </body>
  94. </html>

Creating our Add Code

Lastly, we create our add sale code. We name this code "add_sale.php

"
.

  1. <?php

  2. include

    (

    'conn.php'

    )

    ;
  3. session_start

    (

    )

    ;
  4. $product

    =

    $_POST

    [

    'sales_product'

    ]

    ;
  5. $qty

    =

    $_POST

    [

    'sales_qty'

    ]

    ;
  6. if

    (

    $product

    ==

    0

    )

    {
  7. $_SESSION

    [

    'msg'

    ]

    =

    "Please select a product"

    ;
  8. header

    (

    'location:index.php'

    )

    ;
  9. }
  10. else

    {
  11. //MySQLi Procedural
  12. //mysqli_query($conn,"insert into sales (productid,sales_qty) values ('$product','$qty')");

  13. //MySQLi OOP
  14. $conn

    ->

    query

    (

    "insert into sales (productid,sales_qty) values ('$product

    ','$qty

    ')"

    )

    ;
  15. header

    (

    'location:index.php'

    )

    ;
  16. }
  17. ?>

Demo

That's it! You can now test the simple web application you have created. If you have encountered any errors, please review your code, maybe you have missed some steps.

That's the end of this tutorial. I hope you have learned something useful to adds up to your knowledge on developing a Web Application with the use of PHP programming language.

Explore more on this website for more tutorials and free source codes.

Happy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

344,676

344,684

Top