I'm building a website where students can submit their projects. However, the process_submission.php file isn't working as expected. It's not showing any success or failure messages, and it's not saving the form details to the database. Can you help me fix this?
Using the same db_connection.php file, im able to connect to the relevant database for signup/login.
I've reviewed the file path, permissions, and added error logs, but there's still no indication of errors or success. However, for the signup/login functionality, it does show success or failure to connect to the databases and inserts into the database as intended. The relevant files have been included above, any advice would be appreciated!
student_dash.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Student Dashboard</title>
</head>
<body>
<!-- Student Submission Section -->
<section class="student-submission">
<h2>Make a Submission</h2>
<!-- Main Submission Form -->
<!-- enctype for file inputs, included name attributes -->
<form id="mainSubmissionForm" action="process_submission.php" method="POST" enctype="multipart/form-data">
<label for="projectName">Project Name:</label>
<input type="text" id="projectName" name="projectName" required placeholder="Enter the project name">
<label for="projectSummary">Project Summary:</label>
<textarea id="projectSummary" name="projectSummary" required placeholder="Enter a brief summary of the project"></textarea>
<label for="projectDetails">Project Details:</label>
<textarea id="projectDetails" name="projectDetails" required placeholder="Enter detailed information about the project"></textarea>
<label for="projectField">Project Field:</label>
<select id="projectField" name="projectField" required>
<option value="" disabled selected>Select Project Field</option>
<option value="field1">Field 1</option>
<option value="field2">Field 2</option>
</select>
<label for="projectYear">Project Year:</label>
<select id="projectYear" name="projectYear" required>
<option value="" disabled selected>Select Project Year</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
<!-- File input for video -->
<label for="videoUpload">Video Upload:</label>
<input type="file" id="videoUpload" name="videoUpload" accept="video/*">
<!-- File input for image -->
<label for="imageUpload">Image Upload:</label>
<input type="file" id="imageUpload" name="imageUpload" accept="image/*">
<div class="checkbox-section">
<input type="checkbox" id="includeAuthorDetails" name="includeAuthorDetails">
<label for="includeAuthorDetails">Include Author's Details</label>
</div>
<!-- Author Details Form, hide by default -->
<div class="author-details-form" id="authorDetailsForm" style="display: none;">
<label for="authorFirstName">First Name:</label>
<input type="text" id="authorFirstName" name="authorFirstName" placeholder="Enter Your First Name">
<br>
<label for="authorLastName">Last Name:</label>
<input type="text" id="authorLastName" name="authorLastName" placeholder="Enter Your Last Name">
<br>
<label for="authorEmail">Your Email:</label>
<input type="email" id="authorEmail" name="authorEmail" placeholder="Enter Your Email">
</div>
<!-- Submit Button for Main Submission Form -->
<button type="submit">Submit Project</button>
</form>
</section>
<script defer src="script.js"></script>
<script defer src="ajaxScript.js"></script>
</body>
</html>
process_submission.php:
<?php
// Include the database connection file
include '/Applications/MAMP/htdocs/db_connection.php';
// Initialise error flag
$errorFlag = false;
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$projectName = $_POST["projectName"];
$projectSummary = $_POST["projectSummary"];
$projectDetails = $_POST["projectDetails"];
$projectField = $_POST["projectField"];
$projectYear = $_POST["projectYear"];
$videoUpload = $_FILES["videoUpload"]["name"];
$imageUpload = $_FILES["imageUpload"]["name"];
$authorFirstName = isset($_POST["authorFirstName"]) ? $_POST["authorFirstName"] : "";
$authorLastName = isset($_POST["authorLastName"]) ? $_POST["authorLastName"] : "";
$authorEmail = isset($_POST["authorEmail"]) ? $_POST["authorEmail"] : "";
// Upload files
$targetDir = "uploads/";
move_uploaded_file($_FILES["videoUpload"]["tmp_name"], $targetDir . $videoUpload);
move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $targetDir . $imageUpload);
try {
// Prepare SQL statement
$query = "INSERT INTO projects (projectName, projectSummary, projectDetails, projectField, projectYear, videoUpload, imageUpload, authorFirstName, authorLastName, authorEmail)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($query);
if ($stmt) {
// Bind parameters to prepared statement
$stmt->bind_param("ssssssssss", $projectName, $projectSummary, $projectDetails, $projectField, $projectYear, $videoUpload, $imageUpload, $authorFirstName, $authorLastName, $authorEmail);
// Execute the prepared statement
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: Execution failed";
}
// Close the statement
$stmt->close();
} else {
throw new Exception("Failed to prepare the SQL statement.");
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
}
?>
signup.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Sign Up</title>
</head>
<body>
<form id="signupForm" action="process_signup.php" method="POST">
<h2>New Here?</h2>
<h3>Sign Up</h3>
<div class="textBoxdiv">
<input type="text" name="firstName" placeholder="First Name" required>
</div>
<div class="textBoxdiv">
<input type="text" name="lastName" placeholder="Last Name" required>
</div>
<div class="textBoxdiv">
<input type="email" name="email" placeholder="Email" required>
</div>
<div class="textBoxdiv">
<input type="password" name="password" id="password" placeholder="Password" required>
<!-- hide criteria as default -->
<p id="passwordCriteria" class="criteriaMessage" style="display: none;">Password must: <br> Be at least 8 characters <br> And include: <br> 1 letter <br> 1 number <br> 1 special character</p>
</div>
<button type="submit" class="signupBtn">Sign Up</button>
<div class="login">
<h3>Already have an account?</h3>
<h3><a href="login.php">Login</a></h3>
</div>
</form>
<script defer src="script.js"></script>
<script defer src="ajaxScript.js"></script>
</body>
</html>
process_signup.php:`<?php
<?php
// Include the database connection file
include 'db_connection.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$password = password_hash($_POST["password"], PASSWORD_DEFAULT); // Hash the password
// Insert user data into the 'users' table
$query = "INSERT INTO users (first_name, last_name, email, password) VALUES (?, ?, ?, ?)";
// try-catch block to handle any potential exceptions
try {
$connAuthentication = connectToDatabase("user_authentication");
$stmt = $connAuthentication->prepare($query);
if ($stmt) {
$stmt->bind_param("ssss", $firstName, $lastName, $email, $password);
// Check if the insertion was successful
if ($stmt->execute()) {
// Registration successful
$response = array(
'status' => 'success',
'message' => 'Registration successful!',
);
// Redirect based on user role
$userId = $stmt->insert_id;
$redirectUrl = determineRedirectUrl($userId);
header("Location: $redirectUrl");
exit(); // Ensure script stops after redirection
} else {
// Registration failed
$response = array(
'status' => 'error',
'message' => 'Registration failed. Please try again.',
);
}
// Close the database
$stmt->close();
} else {
throw new Exception("Failed to prepare the SQL statement.");
}
} catch (Exception $e) {
// Handle exceptions, log the error, or display an appropriate message
$response = array(
'status' => 'error',
'message' => 'An error occurred. Please try again later.',
);
}
// Send JSON response
header('Content-Type: application/json');
echo json_encode($response);
} else {
// Invalid request
http_response_code(400);
echo "Invalid request.";
}
// Function to determine the appropriate dashboard based on user role
function determineRedirectUrl($userId) {
$isStudent = true;
if ($isStudent) {
return 'student_dash.html';
} else {
return 'lecturers_dash.html';
}
}
?>
db_connection.php file
<?php
/// Function to create a database connection
function connectToDatabase($databaseName) {
$hostname = "localhost";
$username = "my_user";
$password = "my_pass";
// Create connection
$conn = new mysqli($hostname, $username, $password, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8");
// Temporary logging
error_log("Connection successful to database: $databaseName");
// Avoid echoing here, it can cause issues with JSON responses or HTML output
// echo "Connection successful to database: $databaseName";
return $conn;
}
// Connect to project_database
$connProject = connectToDatabase("project_submission");
// Connect to user_authentication database
$connAuthentication = connectToDatabase("user_authentication");
session_start();
?>