Basic Insert, Delete, Update Database and Image Upload Using PHP

Introduction

PHP can be used to perform common database operations such as inserting, viewing, updating, and deleting records. It can also be used to upload images and store their file names in a database.

In this article, we will explain how to perform basic INSERT, VIEW, UPDATE, and DELETE operations on a MySQL database using PHP, along with a basic example of image upload.

Prerequisites

Before proceeding, make sure you have:

  1. A web server with PHP installed.
  2. MySQL database access.
  3. Basic knowledge of PHP and HTML.
  4. Permission to create a database and table.
  5. A suitable directory to store uploaded images.

Implementation

Step 1: Create the Database and Table

First, create a database named demo and a table named reg to store the user details.

CREATE DATABASE demo;

CREATE TABLE reg (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    address VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    fathername VARCHAR(30) NOT NULL,
    mothername VARCHAR(30) NOT NULL,
    country VARCHAR(50),
    state VARCHAR(30) NOT NULL,
    city VARCHAR(50),
    photo VARCHAR(50)
);

Step 2: Insert Data and Upload an Image

The INSERT operation is used to add a new record to the database.

The PHP code accepts the values submitted through the form and uploads the selected image to the upload directory. The image file name is then stored in the database.

// Insert code

The HTML form should use enctype="multipart/form-data" when uploading files.

<form action="" method="post" enctype="multipart/form-data">

Step 3: View Records

The SELECT query is used to retrieve records from the database.

The VIEW page fetches the records from the reg table and displays the user information in a table. It also provides Edit and Delete links for each record.

// View code

Step 4: Update Records

The UPDATE operation is used to modify an existing record.

The record ID is passed through the URL, the existing information is retrieved, and the submitted values are used to update the corresponding record.

// Edit/Update code

Step 5: Delete Records

The DELETE operation is used to remove a record from the database.

The record ID is passed through the URL, and the corresponding record is deleted from the reg table.

// Delete code

Important Note

The example above uses the older PHP mysql_* functions. These functions have been removed from modern PHP versions.

For new applications, it is recommended to use MySQLi or PDO with prepared statements. Passwords should also be securely hashed instead of storing them as plain text.

For image uploads, validate the file type and size before saving the file to the server.

Conclusion

In this article, we explained how to perform basic database operations using PHP, including insert, view, update, and delete. We also covered how to upload an image and store its file name in a database.

These operations form the basic foundation of many PHP-based applications. For production applications, modern database APIs, prepared statements, password hashing, and secure file-upload validation should be used.

FAQs

1. What are the basic database operations in PHP?

The basic database operations are INSERT, SELECT, UPDATE, and DELETE, commonly referred to as CRUD operations.

2. How can I upload an image using PHP?

An image can be uploaded using an HTML form with multipart/form-data and PHP’s $_FILES variable.

3. Where is the uploaded image stored?

In this example, the uploaded image is stored in the upload directory, while its file name is stored in the database.

Related Article:

How to drop a database in postgreSQL ? – Pheonix Solutions

How to Copy a Table from One Database to Another in MySQL? – Pheonix Solutions

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top