Active and Deactivate account in php

This active and deactivate a account. create a status column in  database table give type int  and  if u insert a database it automatically  take 0 and when select  table give  condition where status="1" 1 means active and 0 is deactivate.
for ex: select * from login where status='1';
Screen-1:Active or De-active account


Screen-2:Active  account


Screen-3:De active  account

Live Demo Download

Create Database:


CREATE DATABASE `demo` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `demo`;

Create Table


CREATE TABLE IF NOT EXISTS `user_mangement` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `status` int(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Insert Table


INSERT INTO `user_mangement` (`id`, `username`, `status`) VALUES
(1, 'Pandi', 1),
(2, 'Karthi', 0),
(3, 'Ram', 1),
(4, 'Arun', 1);

Source Code:

index.php


<html>
<head>
<title>Active and Deactive</title>
</head>
<style>
body {
background:url(images/bg.jpg);
text-align:center;
margin-top:200px;
font-family:Tahoma, Geneva, sans-serif;
}
th {
height:40px;
color:#000;
font-weight:bold;
}
td {
color:#FFF;
height:30px;
}
a {
color:#FFF;
text-decoration:none;
}
a:hover {
color:#FFF;
text-decoration:underline;
}
</style>
<body>
<table width="27%" border="1" align="center" cellpadding="0"
cellspacing="0">
<tr>
<th width="43%" scope="col">User Name</th>
<th width="57%" scope="col">Status</th>
</tr>
<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("demo",$conn) or die(mysql_error());
$select=mysql_query("select * from user_mangement");
while($user=mysql_fetch_array($select))
{
$id=$user['id'];
$username=$user['username'];
$status=$user['status'];
?>
<tr>
<td><?php echo $username?></td>
<td><?php if(($status)=='0') { ?>
<a href="status.php?status=<?php echo $user['id'];?>" onclick="return confirm('Really you activate (<?php echo $username?>)');">
<img src="images/deactivate.png"
id="view" width="16" height="16" alt="" />Deactivated </a>
<?php } if(($status)=='1') { ?>
<a href="status.php?status=<?php echo $user['id'];?>" onclick="return confirm('Really you De-activate (<?php echo $username?>)');">
<img src="images/activate.png" width="16" id="view" height="16" alt="" />Activated</a>
<?php } ?></td><?php }?>
</tr>
</table>
</body>
</html>

status.php


<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("demo",$conn) or die(mysql_error());
if(isset($_GET['status']))
{
$status=$_GET['status'];
$select_status=mysql_query("select * from user_mangement where id='$status'");
while($row=mysql_fetch_object($select_status))
{
$st=$row->status;
if($st=='0')
{
$status2=1;
}
else
{
$status2=0;
}

$update=mysql_query("update user_mangement set status='$status2' where id='$status' ");
if($update)
{
header("Location:index.php");
}
else
{
echo mysql_error();
}
}
}
?>

No comments:

Post a Comment