6 Steps to Create Simple PHP Invoice Script in PHP & MySQL

This is a simple PHP Invoice Script in PHP & MySQL. In this script I’m going to work on these functionalities in our PHP Invoice Script. For creating invoice, we need to have customers and products then invoice generation process. Creating invoices involves different process and it’s bit tricky for beginner developers. Follow these below steps to create PHP based invoice billing system.

1. CRUD Operations for Customers

Customers are the core of any business invoice billing. We should link the invoice to a specific customer. So later on, business owner/manger can check the invoices. In this customers section, I’m not going to provide the code. You have to do it for yourself, I’ll give the outline how you can do it.

You have to create 4 PHP files for customers section to do all CRUD Operations of customers.

  1. addcustomers.php file to add customers – Create Operation
  2. customers.php file to view all customers – Read Operation
  3. editcustomer.php ifle to edit & update customer information – Update Operation
  4. delcustomer.php file to delete customer & customer information – Delete Operation

If you don’t know how to build CRUD application/CRUD Operations, follow this tutorial => Create CRUD Application in PHP & MySQL

By following the above tutorial you can complete all the CRUD operations for customers section. But, before starting you need to plan the customers table. Create customers table with below columns using PHPMyAdmin

  1. ID is integer type and autoincreement
  2. FirstName is varchar type and used to store first name of the customer
  3. LastName is varchar type and used to store last name of the customer
  4. DOB is date type and used to store Date of Birth of customer, you can use this to send birthday notifications through email or sms.
  5. EMail is varchar type and is unique, it is used to store the email of customer to send invoices.
  6. Mobile is varchar type and is unique, it is used to store mobile of customer.
  7. Address is varchar type and used to store address of the customer
  8. City is varchar type and used to store city of the customer.
  9. State is varchar type and used to store state of the customer.
  10. Zip is varchar type and used to store zip code of the customer.
  11. Country is varchar type and used to store country of the customer.

2. CRUD Operations for Products/Services

After finishing above step then only proceed to this step, otherwise you won’t be moving forward. Because, in this step also you have to build it yourself. This is also a CRUD application.

Create 4 files for Products/services based on your need. You just have to create only one that is either product or service.

  1. addproducts.php/addservices.php – Create Operation
  2. products.php/services.php – Read Operation
  3. editproduct.php/editservice.php – Update Operation
  4. delproduct.php/delservice.php – Delete Operation

Create CRUD application based on below columns for products/services table.

  1. ID is integer type and autoincreement
  2. Name is varchar type and is used to store name of the product/service.
  3. Description is varchar type and used to store product/service description.
  4. Stock is varchar type and used to store products stock. It is only used for products not for services.
  5. Cost is varchar type and used to store cost of product/service.

3. Table structure of Invoices & items

I hope you have finished above 2 steps that is customers and at least one of products or services. Then after that create these 2 database tables to store invoices and the other table is invoice items.

Create invoice table with following columns

  1. ID is integer type and autoincreement
  2. UserID is integer, it is used to store customer id
  3. Amount is varchar, it is used to store total invoice amount
  4. PaymentMode is varchar, it is used to store the method of payment.
  5. Timestamp is datetime with default value of current_timestamp, it is used to store the date and time of invoice creation.

Create invoice_items table with following columns

  1. ID is integer type and autoincreement
  2. InvoiceID is integer, it is used to store the id of the invoice from invoice table.
  3. ItemID is integer, it is used to store the id of the product/server from products/services table.
  4. Price is varchar, it is used to store the amount of the product/service.

After creating above tables, then only move forward with next step.

4. Creating Invoice HTML page

Here in this step, I’m going to create invoice generation page. By using this simple bootstrap table html we can create and view the invoice. But here I’m using this code only for creating invoice. You can use this for invoice view and printing invoices.

5. Searching products and adding to invoice

Before searching the invoice we have to pass the user data to invoice PHP page through URL. That URL string can be id of the customer or mobile number of the customer, email of the customer.

If customer profile exists with the information provided through url, then serach products/services input text field will be visible. By using this input field users can search for products/services, after that these products/services can be added to the invoice.

Here I’m using AJAX to search for products/services. If you want to learn in detail, you can learn from here => Create AJAX CRUD Application in PHP & MySQL

After creating AJAX CRUD application, then only use this code.

Add this below code above invoice HTML code. This is simple form code to search for products with onkeypress AJAX function. Before loading this page in the browser, be sure to connect to the database. If you have any problem connecting to database follow this guide => Connect to Mysqli database using PHP5

AJAX function to search for products using the text entered in serach input field from above code. Use this code in script tags.

In above code, I’m using ajax.php file search for products. Create ajax.php with this below code. Include database connection file because this file ajax.php needs database connection.

Replace tr with prodinv id code to below code. This code will fetches the products id from url and select infromation from products table. Displays it in tr.

6. Generating invoice

Before generating the invoice we have to pass all these values in a form with post method. I’ll add this form where we have the payment mode select list. And also calculate total amount and pass the total amount value through hidden input field in below form.

After submitting above form write insert sql query to insert invoice with userid, total amount of invoice and payment mode. After successful insert get the last insert id and write an sql query to insert all the invoice items into database table with invoice id, product id and product price. Do this task on your own.

Conclusion of Simple PHP Invoice Script

While displaying the invoice also, first of all get the invoice based on the id echo all the columns data in proper place. That is invoice date and total amount and customer id. Based on this customer id fetch the customer details from customers table and display it in invoice.

Then product listing part based on the id of the invoice fetch all the items from invoice items table. Using item id fetch the product information from products table and display product name, you already have the product price invoice items table. Just display it in product price html table column.

If you have any problem in any step let me know through comments, I’ll help you.

run php file from command line print invoice of costomer details php code php website maker software php simple login with database php login with existing database login php code free web project compilers dowload script login Coding Cyber script for login in to form
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: