Skip to main content

How to write SQL query with PHP to calculate the sum of sales?

 There are the following steps to create a php project to calculate the sum of sales. 

  

Code of index. php:


<?php


// Database connection


$servername = "localhost";

$username = "root";

$password = "";

$dbname = "marks_sheet";


try{

// Create a connection

$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);

$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){

  echo "Error in connection:".$e->getMessage();

}


// Write the SQL Query to calculate the sum

$sql = "SELECT SUM(amount) AS total_amount FROM sales";

//Execute the query

$stmt = $conn->prepare($sql);

$stmt->execute();

//Fetch the result

$result = $stmt->fetch(PDO::FETCH_ASSOC);?>


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<style type="text/css">

*{

margin:0;

padding:0px;

box-sizing: border-box;

font-family: sans-serif;

}


.container{

display: flex;

width:100%;

height:100vh;

background: whitesmoke;

justify-content: center;

align-items: center;

}

.total{

width:250px;

height:250px;

border-radius:50%;

background:red;

color:white;

font-weight: bold;

justify-content: center;

align-items: center;

line-height:250px;

border:10px solid yellow;

}

</style>

<title>Sales</title>

</head>

<body>

<div class="container">

<div class="total">

<?php

echo "Total Sales Amount:".$result['total_amount'];?>

</div>


</div>


</body>

</html>



<?php

// Close the connection

$conn = null;








?>


Database structure:

Database Name:marks_sheet

Table Name:sales


Final Result:


Download source code:

Download

Comments

Popular posts from this blog

Creating a Student Marksheet using Microsoft Excel.

 Creating a Student Marksheet using Microsoft Excel.  Creating a student marks sheet in MS Excel is straightforward. Here's a step-by-step guide to design one: Step 1: Open MS Excel Launch MS Excel and open a new workbook. Step 2: Structure the Sheet Header Section: Use the first few rows to include the title, such as "Student Marks Sheet" and relevant details like the class, semester, or term. Column Headings: In Row 5 (or below the title), define your column headers: Column A: Roll Number Column B: Student Name Column C onward: Subjects (e.g., Mathematics, Science, English, etc.) Final Columns: Include Total Marks , Percentage , and Grade . Example: Roll No.Student Name Mathematics Science English Total Marks Percentage Grade Step 3: Input Data Enter the roll numbers, student names, and their marks under respective columns. Step 4: Add Formulas Total Marks: In the "Total Marks" column, use the formula to sum marks: =SUM(C2:E2) Copy this ...

Backup one folder to another location

 You can create a simple batch file (.bat) to back up a folder automatically in Windows. Here’s a step-by-step example: 🔹 Example: Backup one folder to another location @echo off set source="C:\MyData" set destination="D:\Backup\MyData_%date:~-4%-%date:~7,2%-%date:~4,2%" echo Backing up from %source% to %destination% xcopy %source% %destination% /E /H /C /I /Y echo Backup completed! pause 🔹 Explanation: set source= → Source folder you want to back up. set destination= → Destination folder where backup will be stored. %date:~-4%-%date:~7,2%-%date:~4,2% adds today’s date in YYYY-MM-DD format to the folder name (so each backup is stored separately). xcopy options: /E → Copy all subdirectories (including empty ones). /H → Copy hidden + system files. /C → Continue even if errors occur. /I → Assume destination is a folder. /Y → Suppress overwrite prompts. 🔹 Usage: Open Notepad . Paste the code above. Save it...

What is Kruti Dev and how to download ?

  Kruti Dev is a popular typeface or font used primarily for typing in the Devanagari script, which is the script for Hindi, Marathi, and other Indian languages. It is widely used in government offices, printing presses, and other organizations in India for Hindi typing. Key Features of Kruti Dev: It is a non-Unicode font, meaning it does not follow the modern Unicode standard. Instead, it uses legacy encoding, which is different from Unicode fonts like Mangal. Kruti Dev fonts are available in various styles and sizes (e.g., Kruti Dev 010, Kruti Dev 020). It requires specific software or a keyboard layout to type effectively. How to Download Kruti Dev Font Official Sources: The font can often be downloaded from trusted websites offering Hindi typing tools or fonts. Ensure the site is reputable to avoid downloading malicious software. Steps to Download: Search for "Download Kruti Dev font" online. Visit a trusted source, such as: www.indiatyping.com www....