我的数据库中有两个表,即导师和学生,结构如下:
tutors table
+------------+--------------------+---------------------+
|tutor_id |tutor_avail_credits |entry_time |
+------------+--------------------+---------------------+
| 20 | 300 | 2017-01-22 11:53:10 |
+------------+--------------------+---------------------+
| 13 | 200 | 2017-01-24 13:58:45 |
+------------+--------------------+---------------------+
students table
+------------+--------------------+---------------------+
|student_id |student_credits |entry_time |
+------------+--------------------+---------------------+
| 20 | 50 | 2017-01-24 12:23:10 |
+------------+--------------------+---------------------+
| 13 | 100 | 2017-01-24 15:58:45 |
+------------+--------------------+---------------------+
我有一个PHP脚本,允许学生预约辅导课程的辅导员,该辅导课程将运行相当于学生学分的分钟。我打算工作的方式是,我运行一个查询来获取最顶层导师的行(最早,因为列表根据时间排序)
$query = "SELECT * FROM tutors
WHERE (tutor_avail_credits > 0 )
ORDER BY entry_time ASC LIMIT 1 ";
如果学生提交表格以保留最高级别的导师,则应从导师学分中减去他/她的学分,直到导师完全保留为止。每次预订发生时, tutor_avail_credits 字段都应该更新,直到它为0.所以我有这段代码。
<?php
//Check if reserve button press
if(isset($_POST['reserve'])){
//Some code here.........
if($student_credits < $tutor_avail_credits){
$tutor_avail_credits = $tutor_avail_credits - $student_credits;
//Update table field.
$update = "UPDATE tutors
SET tutors_avail_credits = '$tutors_avail_credits'
WHERE tutor_id = '$tutor_id'"
}
//Code continues.....
?>
现在的问题是多个用户同时会访问 tutor_avail_credits 字段,这不会导致一些问题吗?如果是这样,我是否可以在导师表中添加其他列,我可以在运行SELECT
查询后设置标志,以便将其用作允许其他用户选择当前未选中的导师的条件这是查询。
$query = "SELECT * FROM tutors
WHERE (tutor_avail_credits > 0 AND status ='available')
ORDER BY entry_time ASC ";
请我初学者指出我正确的方向。