Spaces:
Sleeping
Sleeping
| // Include database connection and authentication | |
| require_once 'includes/db_connect.php'; | |
| require_once 'includes/domain_utils.php'; | |
| require_once 'includes/auth_check.php'; | |
| // Ensure only faculty can access this page | |
| requireFaculty(); | |
| // Check if domain and project ID are provided | |
| if (!isset($_GET['domain']) || !isset($_GET['id'])) { | |
| header("Location: project_manage.php"); | |
| exit; | |
| } | |
| $domainNumber = intval($_GET['domain']); | |
| $projectId = intval($_GET['id']); | |
| $domainTable = "domain_" . $domainNumber; | |
| // Initialize variables | |
| $success = ''; | |
| $error = ''; | |
| $project = null; | |
| // Check if the domain table exists | |
| $tableCheckQuery = "SHOW TABLES LIKE '$domainTable'"; | |
| $tableExists = $conn->query($tableCheckQuery)->num_rows > 0; | |
| if (!$tableExists) { | |
| $error = "Domain table does not exist."; | |
| } else { | |
| // Get the correct project name field for this domain | |
| $projectNameField = getProjectNameField($conn, $domainTable); | |
| // Get the columns of the domain table to determine field structure | |
| $columnsQuery = "SHOW COLUMNS FROM $domainTable"; | |
| $columnsResult = $conn->query($columnsQuery); | |
| $columns = []; | |
| while ($column = $columnsResult->fetch_assoc()) { | |
| $columns[$column['Field']] = $column; | |
| } | |
| // Process form submission | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| // Validate form data | |
| $projectName = isset($_POST['project_name']) ? trim($_POST['project_name']) : ''; | |
| $projectType = isset($_POST['project_type']) ? trim($_POST['project_type']) : ''; | |
| $description = isset($_POST['description']) ? trim($_POST['description']) : ''; | |
| $email = isset($_POST['email']) ? trim($_POST['email']) : ''; | |
| $links = isset($_POST['links']) ? trim($_POST['links']) : ''; | |
| // Additional fields (if any) | |
| $additionalFields = []; | |
| foreach ($_POST as $key => $value) { | |
| if (strpos($key, 'additional_') === 0) { | |
| $fieldName = substr($key, 11); // Remove 'additional_' prefix | |
| $additionalFields[$fieldName] = trim($value); | |
| } | |
| } | |
| // Basic validation | |
| if (empty($projectName) || empty($projectType)) { | |
| $error = "Please provide project name and project type."; | |
| } else { | |
| // Prepare the SQL for update | |
| $updates = []; | |
| $values = []; | |
| $types = ''; | |
| // Add project name field | |
| $updates[] = "`$projectNameField` = ?"; | |
| $values[] = $projectName; | |
| $types .= 's'; // string for Project Name | |
| // Add project type field (H/S) | |
| if (isset($columns['H/S'])) { | |
| $updates[] = "`H/S` = ?"; | |
| $values[] = $projectType; | |
| $types .= 's'; // string for H/S | |
| } | |
| // Add description field | |
| if (isset($columns['Description'])) { | |
| $updates[] = "`Description` = ?"; | |
| $values[] = $description; | |
| $types .= 's'; // string for Description | |
| } | |
| // Add email field | |
| if (isset($columns['Email'])) { | |
| $updates[] = "`Email` = ?"; | |
| $values[] = $email; | |
| $types .= 's'; // string for Email | |
| } | |
| // Add links field | |
| if (isset($columns['Links'])) { | |
| $updates[] = "`Links` = ?"; | |
| $values[] = $links; | |
| $types .= 's'; // string for Links | |
| } | |
| // Add additional fields | |
| foreach ($additionalFields as $field => $value) { | |
| if (isset($columns[$field])) { | |
| $updates[] = "`$field` = ?"; | |
| $values[] = $value; | |
| $types .= 's'; // string for additional fields | |
| } | |
| } | |
| // Add project ID for WHERE clause | |
| $values[] = $projectId; | |
| $types .= 'i'; // integer for Project_ID | |
| // Prepare and execute the update query | |
| $updateQuery = "UPDATE $domainTable SET " . implode(', ', $updates) . " WHERE Project_ID = ?"; | |
| $stmt = $conn->prepare($updateQuery); | |
| if ($stmt) { | |
| // Dynamically bind parameters | |
| $bindParams = array_merge([$types], $values); | |
| $ref_params = []; | |
| foreach ($bindParams as $key => $value) { | |
| $ref_params[$key] = &$bindParams[$key]; | |
| } | |
| call_user_func_array([$stmt, 'bind_param'], $ref_params); | |
| if ($stmt->execute()) { | |
| $success = "Project updated successfully."; | |
| // Refresh project data | |
| $projectQuery = "SELECT * FROM $domainTable WHERE Project_ID = ? LIMIT 1"; | |
| $projectStmt = $conn->prepare($projectQuery); | |
| $projectStmt->bind_param('i', $projectId); | |
| $projectStmt->execute(); | |
| $projectResult = $projectStmt->get_result(); | |
| $project = $projectResult->fetch_assoc(); | |
| $projectStmt->close(); | |
| } else { | |
| $error = "Error updating project: " . $stmt->error; | |
| } | |
| $stmt->close(); | |
| } else { | |
| $error = "Error preparing query: " . $conn->error; | |
| } | |
| } | |
| } else { | |
| // Fetch project details | |
| $projectQuery = "SELECT * FROM $domainTable WHERE Project_ID = ? LIMIT 1"; | |
| $stmt = $conn->prepare($projectQuery); | |
| $stmt->bind_param('i', $projectId); | |
| $stmt->execute(); | |
| $result = $stmt->get_result(); | |
| if ($result && $result->num_rows > 0) { | |
| $project = $result->fetch_assoc(); | |
| } else { | |
| $error = "Project not found."; | |
| } | |
| $stmt->close(); | |
| } | |
| } | |
| // Get domain descriptions | |
| $domainDescriptions = getDomainDescriptions($conn); | |
| $domainDescription = isset($domainDescriptions[$domainNumber]) ? $domainDescriptions[$domainNumber] : "Domain $domainNumber"; | |
| // Include header | |
| include 'includes/header.php'; | |
| <div class="row mb-4"> | |
| <div class="col-md-12"> | |
| <nav aria-label="breadcrumb"> | |
| <ol class="breadcrumb"> | |
| <li class="breadcrumb-item"><a href="index.php">Home</a></li> | |
| <li class="breadcrumb-item"><a href="project_manage.php">Manage Projects</a></li> | |
| <li class="breadcrumb-item active">Edit Project</li> | |
| </ol> | |
| </nav> | |
| <div class="d-flex justify-content-between align-items-center"> | |
| <h2><i class="fas fa-edit me-2"></i> Edit Project</h2> | |
| <div> | |
| <a href="project_details.php?domain=<?php echo urlencode($domainNumber); ?>&id=<?php echo urlencode($projectId); ?>" class="btn btn-info"> | |
| <i class="fas fa-eye me-1"></i> View Project | |
| </a> | |
| <a href="project_manage.php" class="btn btn-secondary"> | |
| <i class="fas fa-arrow-left me-1"></i> Back to List | |
| </a> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php if (!empty($success)): ?> | |
| <div class="alert alert-success"> | |
| <i class="fas fa-check-circle me-2"></i> <?php echo $success; ?> | |
| </div> | |
| <?php endif; ?> | |
| <?php if (!empty($error)): ?> | |
| <div class="alert alert-danger"> | |
| <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?> | |
| </div> | |
| <?php endif; ?> | |
| <?php if ($project): ?> | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <div class="card shadow-sm"> | |
| <div class="card-header bg-primary text-white"> | |
| <h5 class="mb-0"><i class="fas fa-edit me-2"></i> Project Details</h5> | |
| </div> | |
| <div class="card-body"> | |
| <form method="post" action="project_edit.php?domain=<?php echo urlencode($domainNumber); ?>&id=<?php echo urlencode($projectId); ?>" id="projectForm"> | |
| <div class="row"> | |
| <div class="col-md-6 mb-3"> | |
| <label class="form-label">Domain</label> | |
| <input type="text" class="form-control" value="Domain <?php echo htmlspecialchars($domainNumber); ?>: <?php echo htmlspecialchars($domainDescription); ?>" disabled> | |
| </div> | |
| <div class="col-md-6 mb-3"> | |
| <label for="project_type" class="form-label">Project Type *</label> | |
| <select class="form-select" id="project_type" name="project_type" required> | |
| <option value="">Select Type</option> | |
| <option value="Hardware" <?php echo (isset($project['H/S']) && $project['H/S'] === 'Hardware') ? 'selected' : ''; ?>>Hardware</option> | |
| <option value="Software" <?php echo (isset($project['H/S']) && $project['H/S'] === 'Software') ? 'selected' : ''; ?>>Software</option> | |
| </select> | |
| </div> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="project_name" class="form-label">Project Name *</label> | |
| <input type="text" class="form-control" id="project_name" name="project_name" value="<?php echo htmlspecialchars($project[$projectNameField]); ?>" required> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="description" class="form-label">Description</label> | |
| <textarea class="form-control" id="description" name="description" rows="4"><?php echo htmlspecialchars($project['Description'] ?? ''); ?></textarea> | |
| </div> | |
| <div class="row"> | |
| <div class="col-md-6 mb-3"> | |
| <label for="email" class="form-label">Contact Email</label> | |
| <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($project['Email'] ?? ''); ?>"> | |
| </div> | |
| <div class="col-md-6 mb-3"> | |
| <label for="links" class="form-label">Links (Comma separated)</label> | |
| <input type="text" class="form-control" id="links" name="links" value="<?php echo htmlspecialchars($project['Links'] ?? ''); ?>" placeholder="https://example1.com, https://example2.com"> | |
| </div> | |
| </div> | |
| <div id="additional_fields"> | |
| <?php foreach ($project as $field => $value): ?> | |
| <?php if (!in_array($field, ['Project_ID', 'Domain_ID', $projectNameField, 'H/S', 'Description', 'Email', 'Links']) && !empty($field)): ?> | |
| <div class="mb-3"> | |
| <label for="additional_<?php echo htmlspecialchars($field); ?>" class="form-label"><?php echo htmlspecialchars($field); ?></label> | |
| <input type="text" class="form-control" id="additional_<?php echo htmlspecialchars($field); ?>" name="additional_<?php echo htmlspecialchars($field); ?>" value="<?php echo htmlspecialchars($value); ?>"> | |
| </div> | |
| <?php endif; ?> | |
| <?php endforeach; ?> | |
| </div> | |
| <div class="d-grid gap-2 d-md-flex justify-content-md-end mt-4"> | |
| <a href="project_manage.php" class="btn btn-secondary"> | |
| <i class="fas fa-times me-1"></i> Cancel | |
| </a> | |
| <button type="submit" class="btn btn-primary"> | |
| <i class="fas fa-save me-1"></i> Save Changes | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <?php endif; ?> | |
| <?php | |
| // Include footer | |
| include 'includes/footer.php'; | |
| // Close connection | |
| $conn->close(); | |
| ?> |