我在更新功能运行时遇到问题。该函数将徽章标记为可见,以便它们在通知窗口中隐藏。
当用户单击按钮将其标记为可见时,将调用该函数。
我的表上有两个触发器正在尝试更新,我认为这可能会导致问题。
问题是:无法更新存储函数/触发器中的表“用户”,因为它已被调用此存储函数/触发器的语句使用。
触发器:
功能:
function markAsSeen() {
require "connect.php";
$seen = mysqli_query($connection,"Update userbadges
INNER JOIN users ON users.id = userbadges.user_id
SET seen='1'
WHERE studentid = '".$_SESSION["studentid"]."' && seen=0") or die(mysqli_error($connection));
while ($data = mysqli_fetch_array($seen)) {
echo 'Done';
}
}
有什么办法可以解决这个问题吗?
最佳答案
您的问题是 update_users_trigger
触发器对表 users
的内容进行更改,而触发此触发器执行的查询也使用表 users
.
您需要调整查询,以免发生这种死锁。目前尚不清楚每个表中的哪些字段,但我怀疑在您的初始查询中您需要加入 users
这样您就可以在 studentid
上查询.
您可以创建一个不同的函数来获取您需要的用户ID,如下所示:
require_once "connect.php";
function getUserIDFromStudentID($student_id, mysqli $connection)
{
$query = 'SELECT id FROM users WHERE studentid = ? LIMIT 1';
$stmt = $connection->prepare($query);
// Replace the below s to an i if it's supposed to be an integer
$stmt->bind_param("s", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$record = $result->fetch_object();
$result->free();
if ($record) {
return $record->id;
}
}
function markAsSeen(mysqli $connection) {
$user_id = getUserIDFromStudentID($_SESSION["studentid"], $connection);
if (! $user_id) {
throw new Exception('Unable to get user id');
}
$seen_query = 'UPDATE userbadges SET seen = 1 WHERE user_id = ? and seen = 0';
$stmt = $connection->prepare($seen_query);
// Replace the below s to an i if it's supposed to be an integer
$stmt->bind_param("s", $user_id);
$result = $stmt->execute();
if (! $result) {
die(mysqli_error($connection));
}
echo 'Done';
}
传递连接对象而不是每次都需要全局文件将提供更大的灵 active 。
关于php - 功能/触发器已在使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000497/