How to add each data inside a column of a table in both mysql and PHP?

how to display data from database in php in table
how to fetch data from database in php and display in html table
php code to retrieve data from mysql database and display
how to insert data into mysql db using form in php database
how to insert multiple rows in mysql using php form
mysql insert
how to insert data in mysql using php form
how to insert multiple records in mysql using php

I have a column name 'sale' and inside the sale column there are list of sales per customer. how do i add all the sales of each customer and then echo the total sales under the table?

> ?php
include('conn.php');

    $sql = "SELECT * FROM sales";
    $result = mysqli_query($conn,$sql);
    $count = mysqli_num_rows($result);

    if($count>0)
    {
    echo "<html><head></head><body><table border=1>
        <th>CODE</th>
        <th>Employee Name</th>
        <th>Customer</th>
        <th>Sales</th>";

       while($row = mysqli_fetch_assoc($result))
        {  
       echo"<tr>";      
        echo"<td>".$row['empcode']."</td>
             <td>".$row['fullname']."</td>
             <td>".$row['customercode']."</td>
             <td>".$row['sales']."</td></tr>";
        }
       echo"</table>";
        }
?>

You need to add a variable outside of the loop and increment it with every iteration of the loop

$totalSales = 0;

if($count>0)
{
  echo "<html><head></head><body><table border=1>
    <th>CODE</th>
    <th>Employee Name</th>
    <th>Customer</th>
    <th>Sales</th>";

  while($row = mysqli_fetch_assoc($result))
  {  
    echo"<tr>";      
    echo"<td>".$row['empcode']."</td>
         <td>".$row['fullname']."</td>
         <td>".$row['customercode']."</td>
         <td>".$row['sales']."</td></tr>";

     $totalSales += $row['sales'];
    }
   echo"</table>";
}

MySQL SUM - Calculating The Sum of Values, () function calculates the total of 1, 1, 2, and 3. Once you have created a table, you need to add data into it. If you are using phpMyAdmin, you can manually enter this information. First select people, the name of your table listed on the left-hand side. Then on the right-hand side, select the tab called insert and type in the data as shown.

For MySQL you can use SUM group function.

e.g. SELECT SUM(sales) as total FROM table_name;

And for PHP, you can do something like this:

$total = 0;
while($row = mysqli_fetch_assoc($result))
{  
    $total += $row['sales'];
    echo"<tr>";      
    echo"<td>".$row['empcode']."</td>
         <td>".$row['fullname']."</td>
         <td>".$row['customercode']."</td>
         <td>".$row['sales']."</td></tr>";
}
// Print total at the end
echo "<tr>";
echo "<td colspan='3'>Total</td>";
echo "<td>".$total."</td>";
echo "</tr>";

Hope this can help you.

MySQL INSERT, How can I add multiple values in one column in MySQL? Creating Tables inside MySQL Database Using PHP. In the previous chapter we've learned how to create a database on MySQL server. Now it's time to create some tables inside the database that will actually hold the data. A table organizes the information into rows and columns. The SQL CREATE TABLE statement is used to create a table in database.

run

$sql = "SELECT *, SUM(sales) AS total_sale FROMsalesGROUP BYcustomercode";

instead of

$sql = "SELECT * FROM sales";

MySQL Insert Multiple Rows By Practical Examples, First, specify the table name and a list of comma-separated columns inside parentheses after Ignore both the column name and value in the INSERT statement. In this example, each row data is specified as a list of values in the VALUES clause. PHP MySQL Tutorial · Node.js MySQL Tutorial · Python MySQL Tutorial  To add a column in a table in MySQL, we can use ALTER command with add column command. First, let us create a table with columns Id and Name. After that, we will add column name Age and Address with the help of ALTER command. The following is the query to create a table. We can check information about the table with the help of DESC command.

How to Use PHP to Insert Data Into MySQL Database, To insert multiple rows into a table, you use the following form of the INSERT Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause. each element must be the same as the number of columns in the column_list . Following problem there are 2 tables, "Daten" and "Server" in my Mysql DB called "csv" they both got "ServerID" inside them as a column. How can I copy the data in column "ServerID" from the table "Server" to "Daten". I probably have to say the numbers in "Server" "ServerID" are AUTO-Increment.

How to Display MySQL Table Data Tutorial, When creating a table, it is recommended to have one ID column. There are two methods you can use to INSERT data into your MySQL database. if you wish to know what each line of code means, check out our how to  Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword. It also allows you to add the new column after an existing column using the AFTER existing_column clause. If you don’t explicitly specify the position of the new column, MySQL will add it as the last column.

phpMyAdmin Create And Populate Tables Tutorial, Learn how to populate and display MySQL table data with PHP need to use a MySQL table to store data inside it and then output that data by using a PHP script. the data submitted by the user will be passed to a script named insert.​php. This is a basic MySQL query which will tell the script to select all the records from  Table with MySQL Informations Author: Olivier License: FPDF Description This class allows to output a table whose content comes from a MySQL query. The table header is automatically printed on top of each page. The method to add a column is: AddCol([mixed field [, mixed width [, string caption [, string align]]]])

Comments
  • Add a variable to add sales in while loop?