-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign_save.php
69 lines (61 loc) · 2.18 KB
/
assign_save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
include "nav_config.php";
$m_mode = $_REQUEST['m_mode'];
$id = $_REQUEST['id'];
$username = $_REQUEST['username'];
$menu_id = $_REQUEST['menu_id'];
echo $m_mode;
if ($m_mode == "add") {
if (isset($_REQUEST['submit'])) {
$user_id = $_REQUEST['user_id'];
$selected_menu_ids = isset($_REQUEST['menu_id']) ? $_REQUEST['menu_id'] : array();
if (empty($user_id)) {
echo '<script>alert("No user selected.")
window.location.href = "assign_menu.php";
</script>';
exit;
} if (empty($menu_id)) {
echo '<script>alert("No menu selected.")
window.location.href = "assign_menu.php";
</script>';
exit;
}
$query = "SELECT menu_id FROM user_menu WHERE user_id='$user_id'";
$result = $conn->query($query);
$existing_menu_ids = array();
while ($row = $result->fetch_assoc()) {
$existing_menu_ids[] = $row['menu_id'];
}
$new_menu_ids = array_diff($selected_menu_ids, $existing_menu_ids);
$deleted_menu_ids = array_diff($existing_menu_ids, $selected_menu_ids);
foreach ($new_menu_ids as $new_menu_id) {
$query = "INSERT INTO user_menu (user_id, menu_id) VALUES ('$user_id', '$new_menu_id')";
$result = $conn->query($query);
}
foreach ($deleted_menu_ids as $deleted_menu_id) {
$query = "DELETE FROM user_menu WHERE user_id='$user_id' AND menu_id='$deleted_menu_id'";
$results = $conn->query($query);
}
if ($result === TRUE) {
?>
<script>
alert("Records inserted successfully.");
window.location.href = "assign_display.php";
</script>
<?php
} else if ($results === TRUE) {
?>
<script>
alert("Records deleted successfully.");
window.location.href = "assign_display.php";
</script>
<?php
} else
{
echo '<script>alert("Nothing new selected ")
window.location.href = "assign_menu.php";
</script>';
}
}
}
?>