-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_flashcard.php
100 lines (86 loc) · 3.61 KB
/
edit_flashcard.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
// Database connection
include 'db_connection.php';
// Check if flashcard ID and access code are provided in query parameters
if (isset($_GET['flashcard_id']) && isset($_GET['access_code'])) {
$flashcard_id = $_GET['flashcard_id'];
$access_code = $_GET['access_code'];
// Check if the table exists for the given access code
$table_name = mysqli_real_escape_string($conn, $access_code);
$check_table_query = "SHOW TABLES LIKE '$table_name'";
$result = $conn->query($check_table_query);
if ($result->num_rows == 1) {
// Retrieve flashcard information from the database based on the ID and access code
$flashcard_query = "SELECT * FROM $table_name WHERE id = ? LIMIT 1";
$stmt = $conn->prepare($flashcard_query);
$stmt->bind_param("i", $flashcard_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1) {
$flashcard = $result->fetch_assoc();
} else {
header("Location: list.php?access_code=$access_code");
exit;
}
} else {
header("Location: list.php?access_code=$access_code");
exit;
}
} else {
// Redirect to list page if flashcard ID or access code is not provided
header("Location: list.php?access_code=$access_code");
exit;
}
// Process form submission to update flashcard information in the database
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$content = $_POST['content'];
$hint = $_POST['hint'];
$status = $_POST['status'];
// Validate form data
if (!empty($content)) {
// Update flashcard information in the database
$update_query = "UPDATE $table_name SET content = ?, hint = ?, status = ? WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("ssii", $content, $hint, $status, $flashcard_id);
if ($stmt->execute()) {
header("Location: list.php?access_code=$access_code");
exit;
} else {
// Store error message
$executionError = '<div class="info">خطا در ویرایش فلش کارت: ' . $stmt->error . '</div>';
}
} else {
// Store error message
$validationError = '<div class="info">فیلدها نمیتوانند خالی باشند</div>';
}
}
// Header
$page_title = "ویرایش فلش کارت"; include 'header.php';
// Display error message if it exists
if (!empty($executionError)) {
echo $executionError;
}
// Display error message if it exists
if (!empty($validationError)) {
echo $validationError;
}
?>
<form method="post" <?php if ($_GET['access_code'] === 'test') echo 'onsubmit="return false;"'; ?>>
<label for="content">کلمه</label>
<input type="text" id="content" name="content" value="<?php echo $flashcard['content']; ?>" required>
<label for="hint">یادداشت</label>
<textarea id="hint" name="hint" required><?php echo $flashcard['hint']; ?></textarea>
<label for="status">سطح</label>
<select id="status" name="status" required>
<option value="0" <?php if ($flashcard['status'] == 0) echo "selected"; ?>>آسان</option>
<option value="1" <?php if ($flashcard['status'] == 1) echo "selected"; ?>>متوسط</option>
<option value="2" <?php if ($flashcard['status'] == 2) echo "selected"; ?>>دشوار</option>
</select>
<input type="submit" value="ویرایش" class="button <?php if ($_GET['access_code'] === 'test') echo "limited"; ?>">
</form>
</body>
</html>
<!-- Footer -->
</div>
<?php include 'footer.php'; ?>