Creating an activate/deactivate button using PHP and MySQL
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.
- MySQL database with a table containing at least course name and a boolean status value.
- PHP and HTML files.
- XAMPP (or any alternative)
- Open XAMPP control panel and start Apache and MySQL modules.
- 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“.
We 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.
Output: We have created a page with clickable buttons and when we click on the buttons the status changes in the database.
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.