Exchanging information of the table with one another

Greetings, I am a technician specializing in the repair of PCs and laptops. Currently, I am working on developing an offline database website where I can store information about repairs, clients, and invoices. My understanding of SQLi and PHP is quite basic as I have just started learning about them.

At the moment, my database consists of three separate tables that are not connected to each other. To input data into these tables, I use three different forms.

CLIENTS

Id – name – surname – phone – address

REPAIRS

Id – name – surname – phone – address – unit – date – price

INVOICE

Id - paidstatus – date paid - name – surname – phone – address- unit – date – price

Due to my limited knowledge, I currently display the data on three separate pages:

Clients.php
Repairs.php
Invoices.php

Although this setup works for me with my current skills, I understand it's not the best solution. For instance, when adding a new repair, I would like to retrieve client information from the clients table such as: name, surname, phone, and address. Similarly, for generating an invoice, I need data from all the tables.

I believe what I require is Database normalization and I am seeking advice on how to achieve that. Below is an example of how I currently pull data from my tables using PHP code snippets:

Your personalized PHP codes here...

I am curious about how different the process would be if I were to implement normalization. Additionally, I am looking for resources or tutorials that could help me learn more about this and provide downloadable source files.


UPDATE


In response to the provided answer, I am interested in understanding how the tables should be created and joined using SQL. Can someone show me how to perform one-to-one and multiple joins?

When adding a new repair, do I only need to input unit, date, and price in the form? What happens if the client does not exist? Would I have to create a client first?

Perhaps something like:

CLIENT: Old (dropdown) or New (button) (may require a snippet of specific code for implementation)

Then I would proceed to enter the repair data. The invoice can be generated afterwards using the information available from the repair table.

Answer â„–1

If you're searching for a source code, unfortunately, I couldn't locate one, but you can find some helpful diagrams here.

The key concept to remember is to avoid duplicating data across tables. For instance, consider modifying your tables as follows:

CLIENTS

Id – name – surname – phone – address

REPAIRS

Id – client_id – unit – date – price

INVOICE

Id - repair_id - paidstatus – date paid
  1. A relationship of one-to-many exists from clients to repairs, meaning 1 client can have many repairs while each repair is tied to only one client.
  2. Clients to invoice demonstrate a one-to-many relationship through repairs, indicating that a client can receive multiple invoices, and an invoice belongs to just one client.
  3. Repair to invoice establishes a one-to-one connection, which might pose limitations if multiple repairs should link to a single invoice initially.

An alternative approach could involve initiating the process with invoices and introducing a foreign key in repairs linking them to invoices rather than clients. This swap in logic offers more flexibility moving forward:

CLIENTS

Id – name – surname – phone – address

INVOICE

Id - client_id - paidstatus – date paid

REPAIRS

Id – invoice_id – unit – date – price

UPDATE

Assuming you opt to retain the original model instead of the modified version proposed earlier, you would create each table similarly to your current method, ensuring that client_id in repairs aligns with id in clients concerning data type.

You can utilize JOIN operations (refer here for SQL JOIN explanations) to execute a unified query extracting your desired information:

# Retrieve all data
SELECT * FROM invoices i JOIN repairs r ON i.repair_id = r.id JOIN clients c ON r.client_id = c.id

# Extract specific customer-related data
SELECT * FROM invoices i JOIN repairs r ON i.repair_id = r.id WHERE r.client_id = <insert id>

# In case JOIN seems complex, you may reference multiple tables simultaneously
# and connect them via a where clause
SELECT * FROM invoices i, repairs r, clients c WHERE i.repair_id = r.id AND r.client_id = c.id

# Fetch necessary details selectively rather than obtaining entire data at once.
# Opt for gathering unpaid invoices and provide links to pages displaying client and repair specifics associated with those invoices
SELECT * FROM invoices i WHERE i.paidstatus='unpaid'

Initially, you must populate the clients' data first. Then within the repair creation form, include a selection box offering client IDs as values.

// Retrieve records from the database
$query = "select id, name, surname from clients";

// Execute the query
$result = $mysqli->query( $query );

echo("<select name='client_id'>");
while( $row = $result->fetch_assoc() ){
   echo("<option value='" . $row['id'] . "'>" . $row['name'] . " " . $row['surname'] . "</option>";
}
echo("</select>");

It's possible to introduce new client fields into the repair form, although this might complicate database insertion procedures, requiring careful consideration.

In essence, the idea involves sourcing IDs from relevant tables to establish references in other tables, enabling streamlined updates if changes occur in primary data fields like a client's contact number.

Answer â„–2

<a href = "Page.php?View=Invoices">Check Invoices</a>
<a href = "Page.php?View=Repairs">Check Repairs</a>
<a href = "Page.php?View=Clients">Check Clients</a>



<?php

  if (isset($_GET['View'])){

    //retrieve all data from the database
    $query = "select * from ".$mysqli->real_escape_string($_GET['View']);

    //execute the query
    $result = $mysqli->query( $query );
  }

A similar approach can help consolidate views into one page. Ensure compatibility with your database setup and validate views based on $_GET['View']; If:

if ($_GET['View'] === "Test_1"){
  echo "Validate as Clients";
}elseif ($_GET['View'] === "Test_2"){
 echo "Validate as Repairs";
}elseif ($_GET['View'] === "Test_3"){
 echo "Validate as Invoices";
}

This is just an example, make sure to adjust this code according to your specific requirements

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging the framework's event dispatcher to trigger a domain event

When I need to trigger domain events, should I utilize the framework's specific Event Dispatcher or create my own custom Event Dispatcher that is implemented by the framework's Event Dispatcher? Despite the fact that the framework provides a robu ...

What is the process for retrieving external JSON using PHP with a content-type of text/plain?

I am attempting to retrieve an external JSON response using my PHP backend. However, I am encountering an issue where the external endpoint is returned with the Content-Type: text/plain;charset=utf-8, resulting in unreadable content. string 'ï¿½ï¿½ï¿ ...

Managing user sessions for a mobile app login in PHP: Best practices

As a professional PHP programmer, my expertise lies in web development and I have limited knowledge about coding for iOS and Android platforms. Currently, I have created RESTful APIs for two mobile apps, one for iOS and the other for Android, which replic ...

URL rewriting does not retrieve information from the $_GET superglobal

I have defined certain rules for URL rewriting on my website. Currently, I am working on localhost I have configured this in the .htaccess file php_flag output_buffering on Options +FollowSymlinks RewriteEngine On RewriteRule ^p/([^/]*)\.html$ /?p= ...

Arrange charges by title in WooCommerce orders and email alerts

Is there a way to sort fees by name instead of the default sorting by price in WooCommerce orders? I was able to successfully sort fees by name on the cart and checkout pages using the code provided in the answer located at Reordering multiple fees differ ...

How to use global variables in Laravel 5 mail.php configuration

To handle setting the admin email in various locations, I have created a file named constants.php in the config folder. <?php return array( 'admin_email' =>'example@example.com', 'admin_name' =>'Admin&ap ...

Synchronize the Android database with a remote server database

I am currently developing an Android app that utilizes two databases. My goal is to synchronize these databases with two identical databases on a web server, ensuring they have the same field structure and field names. Before syncing, I need to access info ...

PHP problem with authenticating SMTP

After implementing mail1.php page, I encountered an authentication error. <?php include 'class.phpmailer.php'; $mail = new PHPMailer(); // object creation $mail->IsSMTP(); // SMTP enabled $mail->SMTPDebug = 1; // debug leve ...

Containerize your Laravel/VueJS application using Docker

My goal is to dockerize my Laravel application, and I have come across various tutorials that seem helpful: Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 In my development Dockerfile, I have defined the necessary steps for setting up the environment. Ho ...

Exploring the Integration of MySQL with PHP

I have a great tool on my website that displays different pages based on user input. Here is the code for it: if(isset($HTTP_GET_VARS['mod'])) { $page = $HTTP_GET_VARS['mod']; } else { $page = 'home'; } switch($page ...

Following the implementation of the YITH ajax navigation filter, my jQuery scripts are no longer functioning as

I'm having trouble with using yith ajax navigation in my theme. Everything works perfectly until I click on an element to filter, at which point my jquery codes stop working. The team at yith suggests: If your product list contains JavaScript cod ...

Creating a Simple HTML Table Using PHP Arrays

I attempted to create a simple PHP array table, but the output of my code did not meet my expectations. <?php $names = array ( 'First Name' => array('Alfre', 'Beka', 'Charlie'), ...

Creating multiple thumbnails and storing them in various folders using CodeIgniter

I am looking to organize my images into different folders - one for original pictures and another for thumbnails. However, when I try to upload an image to the thumb directory, it shows up as empty. https://i.stack.imgur.com/5Fybz.png Below is the code s ...

What is preventing the successful insertion of a JSON array into a SQL database?

I am facing an issue with inserting a JSON array into an SQL database. I have an HTML table that stores its data in a JavaScript array, converts it to a JSON array, and then sends it to a PHP file. However, when trying to insert this JSON array into the da ...

Parent window login portal

I have just started learning how to program web applications, so I am not familiar with all the technical terms yet. I want to create a login window that behaves like this: When a user clicks on the Login button, a window should pop up on the same page t ...

Deciphering the elements in the periodic table using PHP and HTML

Recently, I encountered a problem that got me thinking. I was given the task of posting people's bios online (as discussed in another question) and I chose to use XML to create elements for each section of the bio. However, when some bios contained ...

Assistance needed for uploading videos using MySQL database and PHP code

Currently, I am in the process of writing PHP code that will be used to both upload and display a Quicktime .mov file by interacting with MySQL. While I have had success with working on similar tasks involving image formats in the past, this particular cir ...

No data returned from Ajax request

I am using jQuery serialize and Ajax to capture form values and process them with JSON as the data type, but I am not getting any values returned. I have tried different approaches to troubleshoot this issue, but so far I haven't been successful. Ther ...

Organize group data by month in a visually appealing table using HTML/PHP with

I have successfully implemented a code that prints HTML table rows from a database table and sorts them by date. The challenge lies in the fact that the date is stored as VARCHAR (due to a pre-existing project with an active database used in a PHP web app ...

Building a Dynamic Web App with PHP and Vue.js

I developed an API using PHP and you can access it through this link: However, I encountered an issue when trying to display the data on the front-end of my Vue.js (Home.vue) file using axios. Below is the code I used: <ul class="ta-track-list" v-if= ...