Creating an activate/deactivate button using PHP and MySQL
Last Updated :
24 Jul, 2024
In this article, we will discuss how to create an Activate/Deactivate button using PHP. When a particular status is set, the same gets updated in the database.
Approach: In order to illustrate the example, let's say that there are a few courses that can be set as active or inactive and need to have appropriate colored buttons for the same. The implementation is done using HTML, PHP, and CSS.
Requirements:
- MySQL database with a table containing at least course name and a boolean status value.
- PHP and HTML files.
- XAMPP (or any alternative)
Database creation:
Open XAMPP control panel and start Apache and MySQL modules.
click on the start buttons
Open "localhost/phpmyadmin" in your browser and click on the new button to create a new database. 
Give a name to your database and click create. 
In the SQL tab enter the following SQL code and click go. 
In order to achieve the desired result, let us create a database called "courses" and set up a few courses using the following SQL code.
Table structure for table `courses`
Course_name String and status boolean fields
CREATE TABLE `courses` (
`Course_name` varchar(50) NOT NULL,
`status` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Sample data for table `courses`
INSERT INTO `courses` (`Course_name`, `status`) VALUES
('C++', 0),
('Java', 1),
('Data Structures', 1),
('SQL', 0);
ALTER TABLE `courses` ADD `id` INT NOT NULL AUTO_INCREMENT
FIRST, ADD PRIMARY KEY (`id`);
In this table, we must know that status is an integer that represents inactive as 0 and active as 1.
Web-page creation: Create a folder in htdocs called courses and store the following file as "course-page.php".
course-page.php
<?php
// Connect to database
$con = mysqli_connect("localhost","root","","courses");
// Get all the courses from courses table
// execute the query
// Store the result
$sql = "SELECT * FROM `courses`";
$Sql_query = mysqli_query($con,$sql);
$All_courses = mysqli_fetch_all($Sql_query,MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<!-- Using internal/embedded css -->
<style>
.btn{
background-color: red;
border: none;
color: white;
padding: 5px 5px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
margin: 4px 2px;
cursor: pointer;
border-radius: 20px;
}
.green{
background-color: #199319;
}
.red{
background-color: red;
}
table,th{
border-style : solid;
border-width : 1;
text-align :center;
}
td{
text-align :center;
}
</style>
</head>
<body>
<h2>Courses Table</h2>
<table>
<!-- TABLE TOP ROW HEADINGS-->
<tr>
<th>Course Name</th>
<th>Course Status</th>
<th>Toggle</th>
</tr>
<?php
// Use foreach to access all the courses data
foreach ($All_courses as $course) { ?>
<tr>
<td><?php echo $course['Course_name']; ?></td>
<td><?php
// Usage of if-else statement to translate the
// tinyint status value into some common terms
// 0-Inactive
// 1-Active
if($course['status']=="1")
echo "Active";
else
echo "Inactive";
?>
</td>
<td>
<?php
if($course['status']=="1")
// if a course is active i.e. status is 1
// the toggle button must be able to deactivate
// we echo the hyperlink to the page "deactivate.php"
// in order to make it look like a button
// we use the appropriate css
// red-deactivate
// green- activate
echo
"<a href=deactivate.php?id=".$course['id']." class='btn red'>Deactivate</a>";
else
echo
"<a href=activate.php?id=".$course['id']." class='btn green'>Activate</a>";
?>
</tr>
<?php
}
// End the foreach loop
?>
</table>
</body>
</html>
Output:
A decent looking table is createdWe are actually not using buttons, rather using hyperlinks that link to PHP files that perform the toggle of the status variable of the table "courses". So we need to create these files too.
activate.php
<?php
// Connect to database
$con=mysqli_connect("localhost","root","","courses");
// Check if id is set or not if true toggle,
// else simply go back to the page
if (isset($_GET['id'])){
// Store the value from get to a
// local variable "course_id"
$course_id=$_GET['id'];
// SQL query that sets the status
// to 1 to indicate activation.
$sql="UPDATE `courses` SET
`status`=1 WHERE id='$course_id'";
// Execute the query
mysqli_query($con,$sql);
}
// Go back to course-page.php
header('location: course-page.php');
?>
deactivate.php
<?php
// Connect to database
$con=mysqli_connect("localhost","root","","courses");
// Check if id is set or not, if true,
// toggle else simply go back to the page
if (isset($_GET['id'])){
// Store the value from get to
// a local variable "course_id"
$course_id=$_GET['id'];
// SQL query that sets the status to
// 0 to indicate deactivation.
$sql="UPDATE `courses` SET
`status`=0 WHERE id='$course_id'";
// Execute the query
mysqli_query($con,$sql);
}
// Go back to course-page.php
header('location: course-page.php');
?>
Output: We have created a page with clickable buttons and when we click on the buttons the status changes in the database.
Similar Reads
Create a Small CRM using PHP and MySQL
CRM stands for Customer Relationship Management, which is a strategy for a set of practices, and a technology designed to manage and analyze customer interactions and data throughout the customer lifecycle. Manage contacts by adding, updating, and deleting information such as name, email, phone, and
6 min read
PHP | MySQL ( Creating Database )
What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty,
3 min read
Creating a Registration and Login System with PHP and MySQL
A registration and login system is a fundamental component of many web applications and provides user authentication and security. This allows users to create an account log in with their login credentials and manage their session securely. By using PHP for server-side scripting and MYSQL for databa
12 min read
PHP | Inserting into MySQL database
Inserting data into a MySQL database using PHP is a crucial operation for many web applications. This process allows developers to dynamically manage and store data, whether it be user inputs, content management, or other data-driven tasks. In this article, We will learn about How to Inserting into
6 min read
Building a REST API with PHP and MySQL
This brief tutorial is a step-by-step guide on how to develop a REST API using PHP and MySQL. REST API will implement HTTP commands (Get, Post, Put, DELETE) and response will be in form of JSON. For development setup, we will be using the XAMPP while for testing of the API, we will use the Postman a
5 min read
CRUD Operation in MySQL Using PHP, Volley Android - Insert Data
It is known that we can use MySQL to use Structure Query Language to store the data in the form of RDBMS. SQL is the most popular language for adding, accessing and managing content in a database. It is most noted for its quick processing, proven reliability, ease, and flexibility of use. The applic
10 min read
How to make a Todo App using PHP & MySQL ?
To create a Todo App using PHP and MySQL, you'll first need to set up a MySQL database to store the tasks. Then, use PHP to create a web interface where users can add, edit, and delete tasks. PHP will handle the backend logic, such as connecting to the database and performing CRUD operations. Finall
4 min read
How to create a Reset Button in form using HTML ?
In this article, we will learn how to create a Reset Button in HTML forms. The reset button is used to reset all the form data values and set them to their initial default value. In case of user enters the wrong data then the user can easily correct it by clicking on the "Reset Button". Syntax:<
1 min read
How to Update Data in MySQL Database Table Using PHP?
Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection
3 min read
PHP | MySQL Database Introduction
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL
4 min read