Add and Delete Users php
There is an administrator in Database management system with edit delete privileges of data base content. and also could manage the database users by creating and deleting users.For this tutorial ,I have created login4 table in my database.
--
-- Table structure for table `login4`
--
CREATE TABLE IF NOT EXISTS `login4` (
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`username` varchar(25) NOT NULL DEFAULT '',
`password` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `login4`
--
INSERT INTO `login4` (`date`, `username`, `password`) VALUES
('2012-07-06 01:12:45', ' Glenn', 'glen123'),
('2012-07-06 01:25:21', 'Angelina ', 'ang123');
Then created admin.php as follows
There is an administrator in Database management system with edit delete privileges of data base content. and also could manage the database users by creating and deleting users.For this tutorial ,I have created login4 table in my database.
--
-- Table structure for table `login4`
--
CREATE TABLE IF NOT EXISTS `login4` (
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`username` varchar(25) NOT NULL DEFAULT '',
`password` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `login4`
--
INSERT INTO `login4` (`date`, `username`, `password`) VALUES
('2012-07-06 01:12:45', ' Glenn', 'glen123'),
('2012-07-06 01:25:21', 'Angelina ', 'ang123');
Then created admin.php as follows
Admin Area
Username: | |
Password: | |
Delete Users
Username: | Delete |
Glenn | |
Angelina |
<?php $connection = mysql_connect("localhost","root",""); if(!$connection){ echo "Couldn't make a connection!!!"; exit; } $db = mysql_select_db("my_database",$connection); if(!$db) { echo "The database disapeared!"; mysql_close($connection); exit; } ?> <html> <head> <title>Login Admin Area</title> </head> <body> <p align="center"><big><big>Admin Area</big></big></p> <table bgcolor=#142984 align="center"> <form method="POST" action="AdminAdd.php"> <input type="hidden" name="react" value="add_user"> <tr bgcolor=#dde2f7><td>Username: </td><td><input type="text" name="user" size="12"></td></tr> <tr bgcolor=#dde2f7><td>Password: </td><td><input type="text" name="pass" size="12"></td></tr> <tr bgcolor=#dde2f7><td colspan=2><input type="submit" value="Add User"></td></tr> </form> </table> <h4><center>Delete Users</center></h4> <form method="POST" action="AdminDel.php"> <input type="hidden" name="react" value="delete_user"> <center><table bgcolor=#142984><tr bgcolor=#dde2f7><td>Username:</td><td>Delete</td></tr> <?php $query = "SELECT username FROM login4 " or die("Cannot Select"); $result = mysql_query($query, $connection); $no= mysql_num_rows($result); if($no) { // we have at least one user, so show all users as options in select form while($row = mysql_fetch_array($result)) { echo "<tr bgcolor=#dde2f7><td>".$row[0]."</td><td><a href='AdminDel.php?id=$row[0]'><img src='user.jpg'></a></td></tr>"; } } else { echo "<tr bgcolor=#dde2f7><td colspan=2>No users created yet</td></tr></table></center>"; } ?> </form> </body> </html> |
AdminAdd.php:-which carries data into database
<?php $username=$_POST['user']; $password=$_POST['pass']; $connection = mysql_connect("localhost","root",""); if(!$connection){ echo "Couldn't make a connection!!!"; exit; } $db = mysql_select_db("my_database",$connection); if(!$db) { echo "The database disappeared!"; mysql_close($connection); exit; } if($username==""||$password=="") { echo "ENTER A USERNAME and PASSWORD for that go to page Admin.php"; exit; } else { $sql="INSERT INTO login4 VALUES(SYSDATE(),'$username','$password')"; $result=mysql_query($sql) or die("Could not Execute"); if($result) { echo ("New user"." ".$username." "."Created"); }} ?> |
AdminDel.php:-to delete records from database
<?php $id=$_GET['id']; if($id=="") { echo "Please go to the first page, AdminIntf.php for Fun"; exit; } $connection = mysql_connect("localhost","root",""); if(!$connection){ echo "Couldn't make a connection!!!"; exit; } $db = mysql_select_db("my_database",$connection); if(!$db) { echo "The database disappeared!"; mysql_close($connection); exit; } $sql= "DELETE FROM login4 where username='$id'"; $result=mysql_query($sql) or die("Could not Execute"); if($result) { echo("The user"." ".$id." "."Successfully Deleted"); } ?> |
No comments:
Post a Comment