Written by Sai Sandeep Thota on November 3, 2013 in php > Programming

Lock a Record to prevent Modification by other users PHP

GET ALERTS:

Get our Latest updates delivered to your mailbox!

When two users try to edit or modify the same record, it results inconsistent data . To get rid of inconsistent data we need to grant access of a record to only one user. If you are a WordPress user, You would have came across an alert box that “User is currently editing this Post.” So we will explain how this works. When a user wants to edit a post he gains the lock over that post and performs changes, after all the modifications are done he will exit so the lock is released and other users are free to modify the record. However WordPress supports multiple users to edit the same record by displaying an Alert message. But in this Tutorial we are not allowing user to modify or update a record unless its lock is revoked.

Lock a Record to prevent Modification by other users PHP

Database (posts.sql)

CREATE TABLE `posts` (
`id` INT NOT NULL AUTO_INCREMENT ,
`post_title` VARCHAR( 255 ) NOT NULL ,
`post_content` VARCHAR( 255 ) NOT NULL ,
`lockedby` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

Connection to Database (connection.php)

<?php
$connect=1;

if($connect=="1") {

$dbhost = localhost;
$dbuser = root;
$dbname = pudemo;

$con=mysql_connect("$dbhost","$dbuser","$dbpass")or die("Cannot Connect!!");
mysql_select_db("$dbname")or die("Cannot select db!!");

}

else
{
//Silence is Golden.
}
?>

Requesting Username (requestusername.php)

Now we will request Username from the the user and redirect him to the Record Editing page.

<html>
<body>
	<form method="POST" action="checkuser.php?id=1">
	Enter your Username: <input type="text" name="username">
	<input type="submit" value="Submit">
	</form>
</body>
</html>

Check User (checkuser.php)

In this page we will request the input from request username. If the record is not locked by anyone for editing the current user gains lock over the record immediately.

<?php
$connect=1;
include("connection.php");

$id = $_GET['id'];


$sql = "SELECT * FROM posts WHERE id=$id";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
	$lockedby = $row['lockedby'];
}

if(empty($lockedby))
{
	$l = $_POST['username'];
	$newlock = mysql_query("UPDATE posts SET lockedby='$l'");
}
else
{
	//Locked by another user!!
}

header("Location:editpost.php?id=1");
?>

Edit Record (editpost.php)

In this page we get the ID of the post in the form of query string from URL and then SQL query is used to fetch the record. If the record has been locked by the same user then he will be able to modify and save the changes else, he will have to wait until the lock is revoked until then he will be able to view the record without any rights to modify it.

<?php
$connect=1;
include("connection.php");

$id = $_GET['id'];
$curUser = $_POST['username'];


$sql = "SELECT * FROM posts WHERE id=$id";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
	$title = $row['post_title'];
	$content = $row['post_content'];
	$lockedby = $row['lockedby'];
}

?>

<html>
<head>
<style>
body {
	font-family: Arial;
	font-size: 12px;
}

strong {
	background: #FFBCBC;
	border: 1px solid #FF7474;
	color: #000;
	padding: 4px;
	margin-bottom: 10px;
}
</style>
</head>
<body>
	<?php
	if(!empty($lockedby)&&($curUser!=$lockedby)) {
		echo '<strong>Alert! This post is currently being edited by '.$lockedby.'. You cannot edit this post now!</strong>';
	}?>
	<form method="POST" action="save.php">
	<table border="0" width="600" style="margin-top:10px;">
		<tr>
			<td>Post Title:</td> 
			<td><input type="text" name="title" value="<?php echo $title; ?>"></td>
		</tr>
		<tr>
			<td>Post Content:</td> 
			<td><textarea name="post_content"><?php echo $content; ?></textarea></td>
			<input type="hidden" value="<?php echo $id; ?>" name="id">
		</tr>

	<?php
	if($curUser == $lockedby)
	{?>
	<tr>
		<td><input type="submit" value="Save & Exit"></td>
	</tr>
	<?php }?>

</table>
</form>
</body>
</html>

post-lock

Save Record (save.php)

In this page the record is updated and the lockedby column will be set to NULL i.e., lock will be revoked.

<?php

$connect=1;
include("connection.php");

$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['post_content'];

$sql = mysql_query("UPDATE posts SET post_title='$title', post_content='$content', lockedby='' WHERE id=$id");

echo 'Update Successful!';
?>

Download Source Code

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: