卸载前正在更新MySQL字段
Updating MySQL field beforeunload
我正在开发一个CMS,允许用户访问MySQL数据库中的记录并对其进行分类。我已经为这个CMS创建了一个用户登录系统。我正在尝试编程一个锁定功能,该功能将在查看记录时进行监控,以便其他用户无法访问此记录。这是为了防止多个用户试图同时写入同一条记录。
到目前为止,这部分工作得还不错。当他们访问记录时,我会更新MySQL数据库中该特定记录的字段"in_use",以显示它正在被查看。当他们提交了带有记录分类的表格时,我会更新字段"in_use"以显示它不再被查看。
我遇到的问题是,当用户在没有提交表单的情况下关闭浏览器时,或者当他们离开页面时。
我一直在尝试使用AJAX来运行beforeunload事件的更新,但它一直不起作用。
*****readRecord.php*****
<h1>Analyze</h1>
//PHP include that contains functions for php queries to the MySQL database.
<?php
include 'core/database/record_functions.php';
?>
//Form to search MySQL database by Case Number and Button to manually release a record that is in_use.
<form action="" method="post">
<p id="case_search">Search records by Case Number: <input type="text" id="case_num_field" name="case_num">
<input type="text" name="in_use" id="in_use" readonly="readonly" hidden="hidden" value="1">
<input type="text" name="user" id="user" readonly="readonly" hidden="hidden" value="<?php echo $_SESSION['username']; ?>">
<input type="submit" name="Submit" value="Select" id="case_num_submit">
<input type="Submit" name="release record" value="Release the current record" onclick="<?php update_in_use($_SESSION['case_num'], 0); ?>">
</p>
</form>
//Includes for jQuery library and AJAX code for automatically releasing the record on page navigation or close.
<script type="text/javascript" src="core/jquery-2.1.3.js"></script>
<script type="text/javascript" src="ajaxBridge.js"></script>
... code continues for reading records
... when the form is submitted update_in_use is called again to set the value to 0
*****ajaxBridge.js*****
// This code is supposed to run the query on releaseVariable.php to update the in_use field to 0 on the beforeunload event.
window.addEventListener("beforeunload", function(event){
if(xmlHttp.readyState==4)
{
xmlHttp.open("POST", "releaseVariable.php", true);
xmlHttp.send();
event.returnValue = "Hey it worked!";
}
event.returnValue = "Hey it failed!";
});
*****releaseVariable.php*****
// This simply runs the update_in_use function to set the in_use field to 0 for this particular case.
<?php
include 'core/init.php';
include 'read_record.php';
protect_page();
update_in_use($_SESSION['case_num'], 0);
?>
我这样做对吗?如果不是,我该如何解决这个问题?
如果这是一个好方法,那么我在哪里误入歧途了代码?
我决定放弃这种方法,转而使用php SESSION变量。
相关文章:
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 单击鼠标,用MySQL数据填充html表单输入字段
- 卸载前正在更新MySQL字段
- 我怎么知道MYSQL统计中哪个字段不匹配
- Jquery使用PHP和MySQL基于一个字段自动完成两个字段
- 获取 MySQL 中某个字段中表达式出现的计数
- PHP 脚本仅从 MySQL 数据库中获取地址字段的第一行
- 使用JQuery,Ajax,PHP和MySQL的多个文本框字段
- 实时表编辑其他 mySQL 表中的显示字段
- 显示数据库 MySQL 描述字段中的 125 个单词
- 如何使用Ajax和MySQL检查多个字段
- 从mysql中获取附加字段并显示在表中
- 根据mysql数据库项的选择禁用输入字段
- 可以't在使用node js将动态字段值插入mysql DB时,在发送头之后设置头
- 在javascript中动态添加字段,并将数据保存到mysql
- 想要在highchart工具提示中获取mysql字段记录
- 当“喜欢”时,使用ajax异步更新mysql字段;点击
- 是否将动态文本框字段值插入数据库?动态创建mysql字段
- 获取一个JavaScript总和答案并将其放在MySQL字段中
- 我如何更新mysql字段时,我的用户失去连接到我的网站