In our previous tale, we learned how the Grand Council of Developers in Codeville designed a robust database structure for their Innovators’ Summit. Now, it’s time to connect the registration form to the database, allowing participants to register and have their information securely stored. Let’s join the council as they integrate the form with the database using PHP and MySQL.
Before connecting the form to the database, the council needed to set up their development environment. They chose to use a LAMP stack (Linux, Apache, MySQL, PHP) for this task.
Install the LAMP Stack
1. Linux: Ensure you have a Linux operating system installed.
Apache: Install Apache to serve web pages.
bash
Copy code
sudo apt-get update
sudo apt-get install apache2
2. MySQL: Install MySQL to manage the database.
bash
Copy code
sudo apt-get install mysql-server
sudo mysql_secure_installation
3. PHP: Install PHP to handle server-side scripting.
bash
Copy code
sudo apt-get install php libapache2-mod-php php-mysql
The council created the necessary database and tables using the SQL scripts designed earlier.
SQL
Copy code
CREATE DATABASE InnovatorsSummit;
USE InnovatorsSummit;
CREATE TABLE Participants (
ParticipantID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL,
ContactNumber VARCHAR(15) NOT NULL,
Track VARCHAR(50) NOT NULL,
DietaryPreferences VARCHAR(255)
);
CREATE TABLE Events (
EventID INT AUTO_INCREMENT PRIMARY KEY,
EventName VARCHAR(100) NOT NULL,
Description TEXT,
EventDate DATE NOT NULL,
EventTime TIME NOT NULL,
Location VARCHAR(100) NOT NULL
);
CREATE TABLE Registrations (
RegistrationID INT AUTO_INCREMENT PRIMARY KEY,
ParticipantID INT,
EventID INT,
RegistrationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ParticipantID) REFERENCES Participants(ParticipantID),
FOREIGN KEY (EventID) REFERENCES Events(EventID)
);
With the database ready, the council moved on to creating a PHP script that would handle form submissions and store the data in the database.
Registration Form (HTML)
Here’s the HTML form from our previous lesson, slightly modified to point to the PHP script:
html
Copy code
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Innovators’ Summit Registration</title>
</head>
<body>
<h1>Innovators’ Summit Registration Form</h1>
<form action=”register.php” method=”post”>
<label for=”name”>Full Name:</label>
<input type=”text” id=”name” name=”name” required>
<br>
<label for=”email”>Email:</label>
<input type=”email” id=”email” name=”email” required>
<br>
<label for=”contact”>Contact Number:</label>
<input type=”tel” id=”contact” name=”contact” required>
<br>
<label for=”track”>Preferred Track:</label>
<select id=”track” name=”track” required>
<option value=”frontend”>Frontend Development</option>
<option value=”backend”>Backend Development</option>
<option value=”fullstack”>Full Stack Development</option>
</select>
<br>
<label for=”diet”>Dietary Preferences:</label>
<input type=”text” id=”diet” name=”diet”>
<br>
<button type=”submit”>Register</button>
</form>
</body>
</html>
Backend Script (PHP)
Here’s the register.php script that handles the form submission and inserts data into the database:
php
Copy code
<?php
// Database configuration
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “InnovatorsSummit”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
// Get form data
$name = $_POST[‘name’];
$email = $_POST[’email’];
$contact = $_POST[‘contact‘];
$track = $_POST[‘track’];
$diet = $_POST[‘diet’];
// Prepare and bind
$stmt = $conn->prepare(“INSERT INTO Participants (Name, Email, ContactNumber, Track, DietaryPreferences) VALUES (?, ?, ?, ?, ?)”);
$stmt->bind_param(“sssss”, $name, $email, $contact, $track, $diet);
// Execute the statement
if ($stmt->execute()) {
echo “Registration successful!”;
} else {
echo “Error: ” . $stmt->error;
}
// Close connection
$stmt->close();
$conn->close();
?>
The council tested the registration form by filling it out and submitting it. They verified that the data was correctly stored in the Participants table of the InnovatorsSummit database.
To further enhance the system, the council considered adding the following features:
Connecting a registration form to a database is a crucial step in managing data for events or applications. By following the steps outlined in this tale, the Grand Council of Developers in Codeville successfully integrated their form with the database, ensuring a smooth and efficient registration process for the Innovators’ Summit.
Feel free to try this yourself and expand upon the basic implementation provided. If you have any questions or need further assistance, I’m here to help. Happy coding!