Viewing file: courses.php (5.81 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<!DOCTYPE html> <html lang="en">
<?php include 'head.php';?>
<body>
<!-- ======= Header ======= --> <?php include 'menubar.php';?>
<?php include 'sidebar.php';?>
<main id="main" class="main">
<div class="pagetitle"> <h1>Dashboard</h1> <nav> <ol class="breadcrumb"> <li class="breadcrumb-item"><a href="index.html">Home</a></li> <li class="breadcrumb-item active">Dashboard</li> </ol> </nav> </div><!-- End Page Title --> <?php // DB connection include 'connect.php';
// Insert/Update logic if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = $_POST['id'] ?? ''; $course_name = $_POST['course_name']; $course_duration = $_POST['course_duration']; $course_elegibility = $_POST['course_elegibility']; $course_code = $_POST['course_code']; $course_content = $_POST['course_content']; $certificate_course = $_POST['certificate_course']; // Handle photo upload $photo_name = ''; if (!empty($_FILES['course_photo']['name'])) { $photo_name = time() . '_' . basename($_FILES['course_photo']['name']); move_uploaded_file($_FILES['course_photo']['tmp_name'], "images/" . $photo_name); }
if ($id) { // Update $sql = "UPDATE courses SET course_name=?, course_duration=?, course_elegibility=?, course_code=?, course_content=?, certificate_course=?"; if ($photo_name) $sql .= ", course_photo=?"; $sql .= " WHERE id=?"; $stmt = $conn->prepare($sql); if ($photo_name) $stmt->bind_param("sssssssi", $course_name, $course_duration, $course_elegibility, $course_code, $course_content, $certificate_course, $photo_name, $id); else $stmt->bind_param("ssssssi", $course_name, $course_duration, $course_elegibility, $course_code, $course_content, $certificate_course, $id); $stmt->execute(); } else { // Insert $stmt = $conn->prepare("INSERT INTO courses (course_name, course_duration, course_elegibility, course_code, course_content, course_photo, certificate_course) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("sssssss", $course_name, $course_duration, $course_elegibility, $course_code, $course_content, $photo_name, $certificate_course); $stmt->execute(); } }
// Delete if (isset($_GET['delete'])) { $id = $_GET['delete']; $conn->query("DELETE FROM courses WHERE id=$id"); header("Location: courses.php"); exit; }
// Fetch course for edit $edit = null; if (isset($_GET['edit'])) { $id = $_GET['edit']; $res = $conn->query("SELECT * FROM courses WHERE id=$id"); $edit = $res->fetch_assoc(); }
// Get all courses $courses = $conn->query("SELECT * FROM courses ORDER BY id DESC"); ?>
<!DOCTYPE html> <html> <head> <title>Manage Courses</title> <style> table { width: 100%; border-collapse: collapse; margin: 20px 0; } th, td { padding: 10px; border: 1px solid #ccc; } th { background-color: #f2f2f2; } input, textarea, select { width: 100%; padding: 8px; margin: 5px 0; } img { width: 60px; height: auto; } .form-section { margin-top: 30px; background: #f9f9f9; padding: 15px; border: 1px solid #ddd; } </style> </head> <body>
<h2>Course List</h2> <table> <tr> <th>ID</th> <th>Photo</th> <th>Name</th> <th>Duration</th> <th>Eligibility</th> <th>Code</th> <th>Certificate</th> <th>Content (Plain Text)</th> <th>Actions</th> </tr> <?php while ($row = $courses->fetch_assoc()): ?> <tr> <td><?= $row['ID'] ?></td> <td><img src="images/<?= $row['course_photo'] ?>" alt=""></td> <td><?= htmlspecialchars($row['course_name']) ?></td> <td><?= htmlspecialchars($row['course_duration']) ?></td> <td><?= htmlspecialchars($row['course_elegibility']) ?></td> <td><?= htmlspecialchars($row['course_code']) ?></td> <td><?= htmlspecialchars($row['certificate_course']) ?></td> <td><?= nl2br(strip_tags($row['course_content'])) ?></td> <td> <a href="?edit=<?= $row['ID'] ?>">Edit</a> <a href="?delete=<?= $row['ID'] ?>" onclick="return confirm('Delete this course?')">Delete</a> </td> </tr> <?php endwhile; ?> </table>
<div class="form-section"> <h3><?= $edit ? "Edit Course ID: " . $edit['ID'] : "Add New Course" ?></h3> <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="id" value="<?= $edit['ID'] ?? '' ?>"> <label>Course Name:</label> <input type="text" name="course_name" value="<?= $edit['course_name'] ?? '' ?>" required>
<label>Duration:</label> <input type="text" name="course_duration" value="<?= $edit['course_duration'] ?? '' ?>" required>
<label>Eligibility:</label> <input type="text" name="course_elegibility" value="<?= $edit['course_elegibility'] ?? '' ?>">
<label>Course Code:</label> <input type="text" name="course_code" value="<?= $edit['course_code'] ?? '' ?>">
<label>Certificate Course:</label> <input type="text" name="certificate_course" value="<?= $edit['certificate_course'] ?? '' ?>">
<label>Content (HTML Allowed):</label> <textarea name="course_content" rows="5"><?= $edit['course_content'] ?? '' ?></textarea>
<label>Photo:</label> <input type="file" name="course_photo"> <?php if (!empty($edit['course_photo'])): ?> <img src="course_photos/<?= $edit['course_photo'] ?>" height="60"> <?php endif; ?>
<br><br> <button type="submit"><?= $edit ? "Update" : "Add" ?> Course</button> </form> </div>
</body> </html>
</main><!-- End #main -->
<?php include 'footer.php';?>
</body>
</html>
|