In this tutorial,you will learn how to create activate and deactivate status in PHP and MySQL on the button click. I have shared some simple steps and easy to develop the code.


Activate and Deactivate in PHP and MySQL

How does it work?


Here, I can create two tables for the teacher(admin) and student (user), teacher has the displayed table with activate and deactivate button. 

The student update with name and class, if the teacher shows some file to a particular student by the class and the teacher will change to activate the account button and student using to login by name and class. If the account is deactivate the student contact the teacher to activate the account.
 
Before getting started, you should create the empty file that you want to write the code or copy the code and all the files follow the .php end extension
 
 
First, create the database connection using PHP and MySQL by the name of dbconfig.php
 
  • index.php 
  • add.php 
  • edit.php 
  • view.php 
  • studentview.php 
  • home.php 
  • logout.php

Use the PHPMyAdmin to create SQL query and create the database and table

Table name - Teacher

Create MySql Database and Table



CREATE TABLE `teachers` 
( 
`id` int(11) NOT NULL, 
`student_name` varchar(255) DEFAULT NULL, 
`class` varchar(255) DEFAULT NULL, 
`status` enum('0','1') DEFAULT '0' 
);


Connect to Mysql Database and PHP



<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname="phptutorial";
$con = new mysqli($servername, $username, $password,$dbname);

if ($con->connect_error) {
  die("Connection failed: " . $con->connect_error);
}
?>


The teacher showed a table to fetch data from MySQL. This function accepts the $con parameter to connect the database and retrieve or fetch all data from the teacher tables in the array method.


index.php - Teacher view



<?php
include_once 'dbconfig.php';


if(isset($_GET['delete_id']))
{
 $sql_query="DELETE FROM teacher WHERE id=".$_GET['delete_id'];
 mysqli_query($con,$sql_query);
 header("Location: $_SERVER[PHP_SELF]");
}
if(isset($_GET['status_id']))
{
 $sql_query="UPDATE teacher SET `status`='".$_GET['status']."' WHERE id=".$_GET['status_id'];
 mysqli_query($con,$sql_query);
 header("Location: $_SERVER[PHP_SELF]");
}

?>



<html>
<head>
<title>Dailyaspirants.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" 
rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" 
crossorigin="anonymous">

<script type="text/javascript">
function edit_id(id)
{
  window.location.href='edit.php?edit_id='+id;
}
function view_id(id)
{
  window.location.href='view.php?view_id='+id;
}
function delete_id(id)
{
 if(confirm('Sure to Delete ?'))
 {
  window.location.href='index.php?delete_id='+id;
 }
}
function status_id(id,status)
{
  window.location.href='index.php?status_id='+id+'&status='+status;
}
</script>
<style>
.center{text-align:center;}
#content{margin-top:40px;}
</style>
</head>
<body>
<div class="container">
<center>

<div id="header">
 <div id="content">
    <label><h1>Dailyaspirants.com</h1></label>
    </div>
</div>

<div id="body">
 <div id="content">
    <table align="center" class="table table-bordered"> 
    <tr>
    <th colspan="6" class="center"><a class="btn btn-secondary" href="add.php">+  add student</a></th>
    </tr>
    <th>Student Id.no</th>
    <th>Id</th>
   
    <th colspan="3">Actions</th>
    </tr>
    <?php
 $sql_query="SELECT * FROM teacher";
 $result_set=mysqli_query($con,$sql_query);
 $studentidadd=1;
 while($row=mysqli_fetch_row($result_set))
 {
  ?>
        <tr>
        <td align="center" ><?php echo $studentidadd; ?></td>
       <td align="center" > <a href="javascript:view_id('<?php echo $row[0]; ?>')"> <?php echo $row[1]; ?> </a> </td>
        <?php if($row[count($row)-1]==1) { ?>
        <td align="center"><a href="javascript:status_id('<?php echo $row[0]; ?>',0)">Deactivate</a></td>
        <?php } else { ?>
        <td align="center"><a href="javascript:status_id('<?php echo $row[0]; ?>',1)">Activate</a></td>
        <?php } ?> 
  <td align="center"><a href="javascript:edit_id('<?php echo $row[0]; ?>')">Edit</a></td>
        <td align="center"><a href="javascript:delete_id('<?php echo $row[0]; ?>')">Delete</a></td>
        </tr>
        <?php
       $studentidadd++;  
 }
 ?>
    </table>
    </div>
</div>

</center>
</div>
</body>
</html>


Activate and Deactivate in PHP and MySQL



Add student - add.php



<?php
include_once 'dbconfig.php';

if(isset($_POST['btn-save']))
{
      $student_name = $_POST['student_name'];
      $class = $_POST['class'];
    
 
$sql_query="INSERT INTO teacher (`student_name`,`class`) VALUES('".$student_name."','".$class."')";
 // sql query for inserting data into database
 if(mysqli_query($con,$sql_query))
 {
  ?>
  <script type="text/javascript">
  alert('teacher added Successfully ');
  window.location.href='index.php';
  </script>
  <?php
 }
 else
 {
  ?>
  <script type="text/javascript">
  alert('Error occured while inserting your data');
  </script>
  <?php
 }
}
?>



<html>
<head>
<title>Dailyaspirants.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container">
<center>

<div id="header">
 <div id="content">
    <label><h1>Dailyaspirants.com</h1></label>
    </div>
</div>

<div id="body">
 <div id="content" class="py-5">
    <form method="post" enctype="multipart/form-data" >
    <table align="center" class="table table-bordered">
    <tr>
    <td align="center"><a href="index.php" class="btn btn-secondary">back to main page</a></td>
    </tr>
    <tr>
    <td>
    <input type="text" class="form-control" id="student_name" name="student_name" required placeholder="Student_name">
    </td>
    </tr>
    <tr>
    <td>
    <input type="text" class="form-control" id="class" name="class" required placeholder="Class">
    </td>
    </tr>
    <tr>
    <td><button class="btn btn-primary" type="submit" name="btn-save"><strong>SAVE</strong></button></td>
    </tr>
    </table>
    </form>
    </div>
</div>

</center>
</div>
</body>
</html>


Activate and Deactivate in PHP and MySQL



Student details - update.php



<?php
include_once 'dbconfig.php';
if(isset($_GET['edit_id']))
{
 $sql_query="SELECT * FROM teacher WHERE id=".$_GET['edit_id'];
 $result_set=mysqli_query($con,$sql_query);
 $fetched_row=mysqli_fetch_array($result_set,MYSQLI_ASSOC);
}
if(isset($_POST['btn-update']))
{
   $id = $_POST['id'];
   $student_name = $_POST['student_name'];
   $class = $_POST['class'];
  // sql query for update data into database
  $sql_query="UPDATE teacher SET `id`='$id',`student_name`='$student_name',`class`='$class' WHERE id=".$_GET['edit_id'];
 if(mysqli_query($con,$sql_query))
 {
  ?>
  <script type="text/javascript">
  alert('teacher updated successfully');
  window.location.href='index.php';
  </script>
  <?php
 }
 else
 {
  ?>
  <script type="text/javascript">
  alert('Error occured updating data');
  </script>
  <?php
 }
}
if(isset($_POST['btn-cancel']))
{
 header("Location: index.php");
}
?>



<html>
<head>
<title>Dailyaspirants.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container">
<center>

<div id="header">
 <div id="content">
    <label><h1>Dailyaspirants.com</h1></label>
    </div>
</div>
<div id="body">
 <div id="content" class="py-5">
    <form method="post" enctype="multipart/form-data">
    <table align="center" class="table table-bordered">
	<tr>
    <td align="center"><a href="index.php" class="btn btn-secondary">back to main page</a></td>
    </tr>
    <tr>
    <td>
    <input type="text" value="<?php echo $fetched_row['id'] ?>" class="form-control" id="id" name="id">
</td>
    </tr>
  <tr>
    <td>
    <input type="text" value="<?php echo $fetched_row['student_name'] ?>" class="form-control" id="student_name" name="student_name">
</td>
    </tr>
  <tr>
    <td>
    <input type="text" value="<?php echo $fetched_row['class'] ?>" class="form-control" id="class" name="class">
</td>
    </tr>
      <tr>
    <td>
    <button class="btn btn-primary" type="submit" name="btn-update"><strong>UPDATE</strong></button>
    <button class="btn btn-danger" type="submit" name="btn-cancel"><strong>Cancel</strong></button>
    </td>
    </tr>
    </table>
    </form>
    </div>
</div>
</center>
</div>
</body>
</html>


Activate and Deactivate in PHP and MySQL



View student details - view.php



<?php
include_once 'dbconfig.php';

if(isset($_GET['view_id']))
{
 $sql_query="SELECT * FROM teacher WHERE id=".$_GET['view_id'];
 $result_set=mysqli_query($con,$sql_query);
 $fetched_row=mysqli_fetch_array($result_set,MYSQLI_ASSOC);
}
?>



<html>
<head>
<title>Dailyaspirants.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container">
<center>

<div id="header">
 <div id="content" class="py-5">
    <label><h1>Dailyaspirants.com</h1></label>
    </div>
</div>


 <table align="center" class="table table-bordered">
 <tr>
    <td align="center"><a href="index.php" class="btn btn-secondary">back to main page</a></td>
 </tr>
   <tr>
   <th colspan="5">id: <?php echo $fetched_row['id'] ?></th>
</tr>
 <tr>
   <th colspan="5">student_name: <?php echo $fetched_row['student_name'] ?></th>
</tr>
 <tr>
   <th colspan="5">class: <?php echo $fetched_row['class'] ?></th>
</tr>
 </table>
</center>
</body>
</html>


Activate and Deactivate in PHP and MySQL



Student Login - login.php



<?php
        session_start();

        include 'dbconfig.php';

        if(isset($_POST['login'])){

        $name = $_POST['name'];
        $class =$_POST['class'];

        $user = "select * from teacher where student_name='$name' AND class='$class'";
		
		$result = mysqli_query($con,$user);
		
		if($result->num_rows >0)
		{
           $row = mysqli_fetch_assoc($result);
           $_SESSION['name'] = $row['student_name'];
           $_SESSION['status'] = $row['status'];
		   if($_SESSION['status']==1)
		   {
			   header("location:home.php");
		   }		   
		   else{
			   echo "<script>alert('please contact teacher for appoval')</script>";
			   }
		}

		}

?>



<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
.container{max-width:600px;margin-top:150px;}
</style>
</head>
<body>

<main class="form-signin">
<div class="container">
  <form action="" method="post">
    <h1 class="h3 mb-3 fw-normal">Please sign in</h1>

    <div class="col-md-8">
	<label for="floatingInput">student Name:</label>
      <input type="text" class="form-control"  name="name" placeholder="student name">
      
    </div>
    <div class="col-md-8">
	<label for="floatingPassword">Class</label>
      <input type="text" class="form-control" name="class" placeholder="class">
      
    </div>
	<br/>
    <input class="w-70 btn btn btn-primary" type="submit" name="login" value="login">
  </form>
  </div>
</main>

</body>

</html>


Activate and Deactivate in PHP and MySQL



student view - home.php



<?php
session_start();
include 'dbconfig.php';
?>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Dailyaspiratns.com > student</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <ul class="navbar-nav ms-auto">
        <li class="nav-item">
          <a class="nav-link" href="#">welcome&nbsp;<strong><?php print_r ($_SESSION['name']);  ?></strong></a>
        </li>
		 <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="logout.php">logout</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<div class="container py-5">
<div class="col">
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
   <p>
   "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
   </p>
</div>
</div>
</body>
</html>


Logout.php



<?php
if (!isset($_SESSION)) { session_start(); }

$_SESSION = $row['student_name']; 

session_destroy(); 

header("Location: studentview.php");
exit();
?>


Activate and Deactivate in PHP and MySQL



Post a Comment

Previous Post Next Post