$value) { if (strpos($key, 'additional_') === 0) { $fieldName = substr($key, 11); // Remove 'additional_' prefix $additionalFields[$fieldName] = trim($value); } } // Basic validation if ($domainNumber <= 0 || empty($projectName) || empty($projectType)) { $error = "Please provide domain, project name, and project type."; } else { // Determine domain table name $domainTable = "domain_" . $domainNumber; // Check if the domain table exists $tableCheckQuery = "SHOW TABLES LIKE '$domainTable'"; $tableExists = $conn->query($tableCheckQuery)->num_rows > 0; if (!$tableExists) { $error = "Selected 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; } // Generate a new project ID $maxIdQuery = "SELECT MAX(Project_ID) as max_id FROM $domainTable"; $maxIdResult = $conn->query($maxIdQuery); $maxId = 1; // Default if no projects exist if ($maxIdResult && $row = $maxIdResult->fetch_assoc()) { $maxId = (!is_null($row['max_id'])) ? intval($row['max_id']) + 1 : 1; } // Prepare the SQL for insertion $fields = ['Project_ID']; $values = [$maxId]; $types = 'i'; // integer for Project_ID // Add Domain_ID field if it exists if (isset($columns['Domain_ID'])) { $fields[] = 'Domain_ID'; $domainId = "D-" . $domainNumber; $values[] = $domainId; $types .= 's'; // string for Domain_ID } // Add project name field $fields[] = $projectNameField; $values[] = $projectName; $types .= 's'; // string for Project Name // Add project type field (H/S) if (isset($columns['H/S'])) { $fields[] = 'H/S'; $values[] = $projectType; $types .= 's'; // string for H/S } // Add description field if (isset($columns['Description']) && !empty($description)) { $fields[] = 'Description'; $values[] = $description; $types .= 's'; // string for Description } // Add email field if (isset($columns['Email']) && !empty($email)) { $fields[] = 'Email'; $values[] = $email; $types .= 's'; // string for Email } // Add links field if (isset($columns['Links']) && !empty($links)) { $fields[] = 'Links'; $values[] = $links; $types .= 's'; // string for Links } // Add additional fields foreach ($additionalFields as $field => $value) { if (isset($columns[$field]) && !empty($value)) { $fields[] = $field; $values[] = $value; $types .= 's'; // string for additional fields } } // Prepare and execute the insert query $fieldStr = implode(', ', array_map(function($field) { return "`$field`"; }, $fields)); $placeholders = implode(', ', array_fill(0, count($fields), '?')); $insertQuery = "INSERT INTO $domainTable ($fieldStr) VALUES ($placeholders)"; $stmt = $conn->prepare($insertQuery); if ($stmt) { // Dynamically bind parameters $params = array_merge([$types], $values); $ref_params = []; foreach ($params as $key => $value) { $ref_params[$key] = &$params[$key]; } call_user_func_array([$stmt, 'bind_param'], $ref_params); if ($stmt->execute()) { $success = "Project added successfully with ID: $maxId"; // Clear form after successful submission $projectName = $description = $email = $links = ''; $projectType = ''; $additionalFields = []; } else { $error = "Error adding project: " . $stmt->error; } $stmt->close(); } else { $error = "Error preparing query: " . $conn->error; } } } } // Include header include 'includes/header.php'; ?>