Locking a record from being edited by setting a Lock field [duplicate]
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
1 answer
How to display errors for my MySQLi query? [duplicate]
3 answers
Reference - What does this error mean in PHP?
32 answers
I am right now working on a system that pulls records from a table in a database, that displays and manipulates this info in php. I want to lock certain records from being changed, if that specific record is in use.
So in this table; I have a 'Lock' field. Whenever I have a record that i want to edit, I want to display the information in a form, and once that is done, I want to update the Lock field to "in Use". When another user tries to access that file to edit it, the php file i am running to populate form, should first check if the Lock field is filled. They are then redirected and given a message if the Lock field is indeed filled.
At the moment, I am having some difficulty with 1: Updating and setting the Lock field, and 2: making sure that any user (either in another browser, or different computer) is denied access to the record.
For the moment this is my code:
<?php
require 'connection information.php';
$conn = mysqli_connection($server, $username, $password);
$FileID = 123;
$checkLock = "SELECT RecordLock FROM `dataTable` WHERE FileID = '".$FileID."'";
$LockResult = mysqli_query($conn, $checkLock);
if(!empty(mysqli_fetch_array($LockResult)){ // the Lock is anything but empty the user will be shown this message
echo "this record is in use. try again later.";
}
else{// The lock is empty, they can edit the record
$getDatabaseInfo = "SELECT * FROM dataTable WHERE FileID = '".$FileID."'";
$databaseResults = mysqli_query($conn,$getDatabaseInfo);
while($mysqli_fetch_array($databaseResults)){
// this is where I auto populate/generate a form with all the information
}
$SetLock = "UPDATE dataTable SET RecordLock='*User's ID*' WHERE FileID ='".$FileID."'";
mysqli_query($conn,$SetLock);
}
?>
Right now, I cannot manage to make the $SetLock query to pass, and truly edit the Lock field to "In Use".
Am I doing this right, is there a more elegant way of doing this?
Edit 1: added $conn, to Mysqli_query($SetLock);.
Edit 2: changed "Lock" queries to RecordLock.
php mysql
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 0:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
1 answer
How to display errors for my MySQLi query? [duplicate]
3 answers
Reference - What does this error mean in PHP?
32 answers
I am right now working on a system that pulls records from a table in a database, that displays and manipulates this info in php. I want to lock certain records from being changed, if that specific record is in use.
So in this table; I have a 'Lock' field. Whenever I have a record that i want to edit, I want to display the information in a form, and once that is done, I want to update the Lock field to "in Use". When another user tries to access that file to edit it, the php file i am running to populate form, should first check if the Lock field is filled. They are then redirected and given a message if the Lock field is indeed filled.
At the moment, I am having some difficulty with 1: Updating and setting the Lock field, and 2: making sure that any user (either in another browser, or different computer) is denied access to the record.
For the moment this is my code:
<?php
require 'connection information.php';
$conn = mysqli_connection($server, $username, $password);
$FileID = 123;
$checkLock = "SELECT RecordLock FROM `dataTable` WHERE FileID = '".$FileID."'";
$LockResult = mysqli_query($conn, $checkLock);
if(!empty(mysqli_fetch_array($LockResult)){ // the Lock is anything but empty the user will be shown this message
echo "this record is in use. try again later.";
}
else{// The lock is empty, they can edit the record
$getDatabaseInfo = "SELECT * FROM dataTable WHERE FileID = '".$FileID."'";
$databaseResults = mysqli_query($conn,$getDatabaseInfo);
while($mysqli_fetch_array($databaseResults)){
// this is where I auto populate/generate a form with all the information
}
$SetLock = "UPDATE dataTable SET RecordLock='*User's ID*' WHERE FileID ='".$FileID."'";
mysqli_query($conn,$SetLock);
}
?>
Right now, I cannot manage to make the $SetLock query to pass, and truly edit the Lock field to "In Use".
Am I doing this right, is there a more elegant way of doing this?
Edit 1: added $conn, to Mysqli_query($SetLock);.
Edit 2: changed "Lock" queries to RecordLock.
php mysql
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 0:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
2
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50
|
show 2 more comments
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
1 answer
How to display errors for my MySQLi query? [duplicate]
3 answers
Reference - What does this error mean in PHP?
32 answers
I am right now working on a system that pulls records from a table in a database, that displays and manipulates this info in php. I want to lock certain records from being changed, if that specific record is in use.
So in this table; I have a 'Lock' field. Whenever I have a record that i want to edit, I want to display the information in a form, and once that is done, I want to update the Lock field to "in Use". When another user tries to access that file to edit it, the php file i am running to populate form, should first check if the Lock field is filled. They are then redirected and given a message if the Lock field is indeed filled.
At the moment, I am having some difficulty with 1: Updating and setting the Lock field, and 2: making sure that any user (either in another browser, or different computer) is denied access to the record.
For the moment this is my code:
<?php
require 'connection information.php';
$conn = mysqli_connection($server, $username, $password);
$FileID = 123;
$checkLock = "SELECT RecordLock FROM `dataTable` WHERE FileID = '".$FileID."'";
$LockResult = mysqli_query($conn, $checkLock);
if(!empty(mysqli_fetch_array($LockResult)){ // the Lock is anything but empty the user will be shown this message
echo "this record is in use. try again later.";
}
else{// The lock is empty, they can edit the record
$getDatabaseInfo = "SELECT * FROM dataTable WHERE FileID = '".$FileID."'";
$databaseResults = mysqli_query($conn,$getDatabaseInfo);
while($mysqli_fetch_array($databaseResults)){
// this is where I auto populate/generate a form with all the information
}
$SetLock = "UPDATE dataTable SET RecordLock='*User's ID*' WHERE FileID ='".$FileID."'";
mysqli_query($conn,$SetLock);
}
?>
Right now, I cannot manage to make the $SetLock query to pass, and truly edit the Lock field to "In Use".
Am I doing this right, is there a more elegant way of doing this?
Edit 1: added $conn, to Mysqli_query($SetLock);.
Edit 2: changed "Lock" queries to RecordLock.
php mysql
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
1 answer
How to display errors for my MySQLi query? [duplicate]
3 answers
Reference - What does this error mean in PHP?
32 answers
I am right now working on a system that pulls records from a table in a database, that displays and manipulates this info in php. I want to lock certain records from being changed, if that specific record is in use.
So in this table; I have a 'Lock' field. Whenever I have a record that i want to edit, I want to display the information in a form, and once that is done, I want to update the Lock field to "in Use". When another user tries to access that file to edit it, the php file i am running to populate form, should first check if the Lock field is filled. They are then redirected and given a message if the Lock field is indeed filled.
At the moment, I am having some difficulty with 1: Updating and setting the Lock field, and 2: making sure that any user (either in another browser, or different computer) is denied access to the record.
For the moment this is my code:
<?php
require 'connection information.php';
$conn = mysqli_connection($server, $username, $password);
$FileID = 123;
$checkLock = "SELECT RecordLock FROM `dataTable` WHERE FileID = '".$FileID."'";
$LockResult = mysqli_query($conn, $checkLock);
if(!empty(mysqli_fetch_array($LockResult)){ // the Lock is anything but empty the user will be shown this message
echo "this record is in use. try again later.";
}
else{// The lock is empty, they can edit the record
$getDatabaseInfo = "SELECT * FROM dataTable WHERE FileID = '".$FileID."'";
$databaseResults = mysqli_query($conn,$getDatabaseInfo);
while($mysqli_fetch_array($databaseResults)){
// this is where I auto populate/generate a form with all the information
}
$SetLock = "UPDATE dataTable SET RecordLock='*User's ID*' WHERE FileID ='".$FileID."'";
mysqli_query($conn,$SetLock);
}
?>
Right now, I cannot manage to make the $SetLock query to pass, and truly edit the Lock field to "In Use".
Am I doing this right, is there a more elegant way of doing this?
Edit 1: added $conn, to Mysqli_query($SetLock);.
Edit 2: changed "Lock" queries to RecordLock.
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
1 answer
How to display errors for my MySQLi query? [duplicate]
3 answers
Reference - What does this error mean in PHP?
32 answers
php mysql
php mysql
edited Jan 2 at 2:42
Damian Morris
asked Jan 1 at 1:12
Damian MorrisDamian Morris
72
72
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 0:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 0:14
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
2
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50
|
show 2 more comments
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
2
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
2
2
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50
|
show 2 more comments
2 Answers
2
active
oldest
votes
The biggest problem I see a the moment is you are not tracking what user the table is locked for.
Chances are things will happen over multiple requests
- show record (1 request)
- save record (1 request)
This is the important because how state works for the client > server relationship.
Probably what's happening is you set that record to be locked, and now when you view it, it does exactly what you told it to do which is to echo this out:
echo "this record is in use. try again later.";
The thing you are missing is this:
$checkLock = "SELECT Lock FROM dataTable WHERE FileID = '".$FileID."' AND user_id != $currentUserId";
How you get there I don't know, as I don't know the schema of your DB and you must add a user field in to track that data.
In any case you need this information when the user returns to save the edit, then you can exclude them from the query results and the table will "appear" unlocked for them (so they can edit it).
I don't see any other way you could do it without knowing that.
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
add a comment |
Just looking at the code, you are missing the $conn variable from your last call to the mysqli_query function.
There are more elegant solutions to locking records as explained by other people, so it may be worth looking in to them.
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The biggest problem I see a the moment is you are not tracking what user the table is locked for.
Chances are things will happen over multiple requests
- show record (1 request)
- save record (1 request)
This is the important because how state works for the client > server relationship.
Probably what's happening is you set that record to be locked, and now when you view it, it does exactly what you told it to do which is to echo this out:
echo "this record is in use. try again later.";
The thing you are missing is this:
$checkLock = "SELECT Lock FROM dataTable WHERE FileID = '".$FileID."' AND user_id != $currentUserId";
How you get there I don't know, as I don't know the schema of your DB and you must add a user field in to track that data.
In any case you need this information when the user returns to save the edit, then you can exclude them from the query results and the table will "appear" unlocked for them (so they can edit it).
I don't see any other way you could do it without knowing that.
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
add a comment |
The biggest problem I see a the moment is you are not tracking what user the table is locked for.
Chances are things will happen over multiple requests
- show record (1 request)
- save record (1 request)
This is the important because how state works for the client > server relationship.
Probably what's happening is you set that record to be locked, and now when you view it, it does exactly what you told it to do which is to echo this out:
echo "this record is in use. try again later.";
The thing you are missing is this:
$checkLock = "SELECT Lock FROM dataTable WHERE FileID = '".$FileID."' AND user_id != $currentUserId";
How you get there I don't know, as I don't know the schema of your DB and you must add a user field in to track that data.
In any case you need this information when the user returns to save the edit, then you can exclude them from the query results and the table will "appear" unlocked for them (so they can edit it).
I don't see any other way you could do it without knowing that.
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
add a comment |
The biggest problem I see a the moment is you are not tracking what user the table is locked for.
Chances are things will happen over multiple requests
- show record (1 request)
- save record (1 request)
This is the important because how state works for the client > server relationship.
Probably what's happening is you set that record to be locked, and now when you view it, it does exactly what you told it to do which is to echo this out:
echo "this record is in use. try again later.";
The thing you are missing is this:
$checkLock = "SELECT Lock FROM dataTable WHERE FileID = '".$FileID."' AND user_id != $currentUserId";
How you get there I don't know, as I don't know the schema of your DB and you must add a user field in to track that data.
In any case you need this information when the user returns to save the edit, then you can exclude them from the query results and the table will "appear" unlocked for them (so they can edit it).
I don't see any other way you could do it without knowing that.
The biggest problem I see a the moment is you are not tracking what user the table is locked for.
Chances are things will happen over multiple requests
- show record (1 request)
- save record (1 request)
This is the important because how state works for the client > server relationship.
Probably what's happening is you set that record to be locked, and now when you view it, it does exactly what you told it to do which is to echo this out:
echo "this record is in use. try again later.";
The thing you are missing is this:
$checkLock = "SELECT Lock FROM dataTable WHERE FileID = '".$FileID."' AND user_id != $currentUserId";
How you get there I don't know, as I don't know the schema of your DB and you must add a user field in to track that data.
In any case you need this information when the user returns to save the edit, then you can exclude them from the query results and the table will "appear" unlocked for them (so they can edit it).
I don't see any other way you could do it without knowing that.
answered Jan 1 at 1:37
ArtisticPhoenixArtisticPhoenix
15.8k11223
15.8k11223
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
add a comment |
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
Thanks for your input, I'll definitely consider it. But are there any other problems you can identify in my code?
– Damian Morris
Jan 1 at 1:52
add a comment |
Just looking at the code, you are missing the $conn variable from your last call to the mysqli_query function.
There are more elegant solutions to locking records as explained by other people, so it may be worth looking in to them.
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
add a comment |
Just looking at the code, you are missing the $conn variable from your last call to the mysqli_query function.
There are more elegant solutions to locking records as explained by other people, so it may be worth looking in to them.
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
add a comment |
Just looking at the code, you are missing the $conn variable from your last call to the mysqli_query function.
There are more elegant solutions to locking records as explained by other people, so it may be worth looking in to them.
Just looking at the code, you are missing the $conn variable from your last call to the mysqli_query function.
There are more elegant solutions to locking records as explained by other people, so it may be worth looking in to them.
answered Jan 1 at 23:01
RobFosRobFos
864621
864621
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
add a comment |
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
thanks for that catch.
– Damian Morris
Jan 2 at 0:05
add a comment |
Welcome. There are many other Q&A posts in Stackoverflow about locking MYSQL databases. Have you looked at or considered any of these?
– Tedinoz
Jan 1 at 1:23
none of them have what i'm looking for. Because I'm having my users edit records through forms, my understanding of transactions says that this method wont work. And Locking a record, as the user edits wont work either. As of my research, all other methods of datatable/database level locking won't work. Am I missing a possible method?
– Damian Morris
Jan 1 at 1:28
Also, I want this locking method to work regardless if the database is InnoDB or myISAM. To my understanding this method is super soft, but I believe is the best way to do it.
– Damian Morris
Jan 1 at 1:29
2
What happens when someone closes the browser with a record locked? (this is a trick question) Also How do you know who locked it, how can any user edit it if it's locked if you don't know who it's locked for?
– ArtisticPhoenix
Jan 1 at 1:30
Thanks for asking the trick question, I really need to consider that. But for the sake of the question, i had to anonimize parts of the system. But in the full system, you can identify who is access the system.
– Damian Morris
Jan 1 at 1:50