Viewing file: add_question.php (6.38 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<!DOCTYPE html> <html lang="en">
<?php include 'head.php';?> <style> table { border-collapse: collapse; width: 100%; margin-top: 20px; }
table, th, td { border: 1px solid #ddd; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; font-weight: bold; } .custom-image { width: 100px; height: 100px; } </style> <body>
<!-- ======= Header ======= --> <?php include 'menubar.php';?>
<?php include 'sidebar.php';?>
<main id="main" class="main">
<?php // Database connection include 'connect.php';
// Handle Add / Update if (isset($_POST['save'])) { $id = isset($_POST['id']) ? intval($_POST['id']) : 0; $question = $conn->real_escape_string($_POST['question']); $wrong1 = $conn->real_escape_string($_POST['wronganswer1']); $wrong2 = $conn->real_escape_string($_POST['wronganswer2']); $wrong3 = $conn->real_escape_string($_POST['wronganswer3']); $wrong4 = $conn->real_escape_string($_POST['wronganswer4']); $correct = $conn->real_escape_string($_POST['correctanswer']); $paperset = $conn->real_escape_string($_POST['paper_set']);
if ($id > 0) { $sql = "UPDATE question_set_paper SET question='$question', wronganswer1='$wrong1', wronganswer2='$wrong2', wronganswer3='$wrong3', wronganswer4='$wrong4', correctanswer='$correct', paper_set='$paperset' WHERE ID=$id"; $conn->query($sql); } else { $sql = "INSERT INTO question_set_paper (question,wronganswer1,wronganswer2,wronganswer3,wronganswer4,correctanswer,paper_set) VALUES ('$question','$wrong1','$wrong2','$wrong3','$wrong4','$correct','$paperset')"; $conn->query($sql); } header("Location: question_crud.php"); exit(); }
// Handle Delete if (isset($_GET['delete'])) { $id = intval($_GET['delete']); $conn->query("DELETE FROM question_set_paper WHERE ID=$id"); header("Location: question_crud.php"); exit(); }
// Fetch all paper sets for filter $paperSets = $conn->query("SELECT DISTINCT paper_set FROM question_set_paper");
// Filter questions $filter = isset($_GET['paper_set']) ? $conn->real_escape_string($_GET['paper_set']) : ""; $where = $filter ? "WHERE paper_set='$filter'" : ""; $result = $conn->query("SELECT * FROM question_set_paper $where ORDER BY ID DESC");
// For edit $editData = null; if (isset($_GET['edit'])) { $id = intval($_GET['edit']); $editData = $conn->query("SELECT * FROM question_set_paper WHERE ID=$id")->fetch_assoc(); } ?> <!DOCTYPE html> <html> <head> <title>Question Bank Management</title> <style> body { font-family: Arial; margin: 20px; } table { border-collapse: collapse; width: 100%; margin-top: 10px; } th, td { border: 1px solid #ccc; padding: 8px; text-align: left; } th { background: #f4f4f4; } .btn { padding: 5px 10px; text-decoration: none; border: 1px solid #333; border-radius: 3px; } .btn:hover { background: #ddd; } .form-box { background: #f9f9f9; padding: 15px; border: 1px solid #ccc; margin-top: 10px; } </style> </head> <body>
<h2>Question Bank</h2>
<!-- Add Button --> <a href="question_crud.php?add=1" class="btn">➕ Add New Question</a>
<!-- Filter --> <form method="get" style="margin-top:10px;"> <label>Filter by Paper Set:</label> <select name="paper_set" onchange="this.form.submit()"> <option value="">All</option> <?php while($ps = $paperSets->fetch_assoc()) { ?> <option value="<?php echo $ps['paper_set']; ?>" <?php if($filter==$ps['paper_set']) echo "selected"; ?>> <?php echo $ps['paper_set']; ?> </option> <?php } ?> </select> </form>
<?php if (isset($_GET['add']) || $editData) { ?> <!-- Add/Edit Form --> <div class="form-box"> <h3><?php echo $editData ? "Edit Question" : "Add Question"; ?></h3> <form method="post"> <input type="hidden" name="id" value="<?php echo $editData['ID'] ?? 0; ?>"> <p>Question: <br><textarea name="question" rows="3" cols="80" required><?php echo $editData['question'] ?? ''; ?></textarea></p> <p>Wrong Answer 1: <input type="text" name="wronganswer1" value="<?php echo $editData['wronganswer1'] ?? ''; ?>" required></p> <p>Wrong Answer 2: <input type="text" name="wronganswer2" value="<?php echo $editData['wronganswer2'] ?? ''; ?>" required></p> <p>Wrong Answer 3: <input type="text" name="wronganswer3" value="<?php echo $editData['wronganswer3'] ?? ''; ?>" required></p> <p>Wrong Answer 4: <input type="text" name="wronganswer4" value="<?php echo $editData['wronganswer4'] ?? ''; ?>" required></p> <p>Correct Answer: <input type="text" name="correctanswer" value="<?php echo $editData['correctanswer'] ?? ''; ?>" required></p> <p>Paper Set: <input type="text" name="paper_set" value="<?php echo $editData['paper_set'] ?? ''; ?>" required></p> <p><button type="submit" name="save" class="btn">💾 Save</button> <a href="question_crud.php" class="btn">Cancel</a></p> </form> </div> <?php } else { ?> <!-- Question List --> <table> <tr> <th>ID</th> <th>Question</th> <th>Wrong 1</th> <th>Wrong 2</th> <th>Wrong 3</th> <th>Wrong 4</th> <th>Correct</th> <th>Paper Set</th> <th>Actions</th> </tr> <?php while($row = $result->fetch_assoc()) { ?> <tr> <td><?php echo $row['ID']; ?></td> <td><?php echo htmlspecialchars($row['question']); ?></td> <td><?php echo $row['wronganswer1']; ?></td> <td><?php echo $row['wronganswer2']; ?></td> <td><?php echo $row['wronganswer3']; ?></td> <td><?php echo $row['wronganswer4']; ?></td> <td><b><?php echo $row['correctanswer']; ?></b></td> <td><?php echo $row['paper_set']; ?></td> <td> <a href="question_crud.php?edit=<?php echo $row['ID']; ?>" class="btn">✏️ Edit</a> <a href="question_crud.php?delete=<?php echo $row['ID']; ?>" class="btn" onclick="return confirm('Delete this question?')">🗑 Delete</a> </td> </tr> <?php } ?> </table> <?php } ?>
</body> </html>
</main><!-- End #main -->
<?php include 'footer.php';?>
</body>
</html>
|