When storing data from a form submission in a MySQL table, is it better to link personal information with course IDs or store course IDs with person IDs in a separate table?
When storing data from a form submission in a MySQL table, it is better to store course IDs with person IDs in a separate table rather than linking personal information with course IDs directly. This approach follows database normalization principles and allows for more efficient querying and data management.
// Assuming you have a 'persons' table and a 'courses' table already created in your database
// Inserting form submission data into 'persons' table
$personName = $_POST['person_name'];
$personEmail = $_POST['person_email'];
$sql = "INSERT INTO persons (name, email) VALUES ('$personName', '$personEmail')";
$result = mysqli_query($connection, $sql);
// Getting the person ID of the inserted record
$personId = mysqli_insert_id($connection);
// Inserting course ID and person ID into a separate table 'person_courses'
$courseId = $_POST['course_id'];
$sql = "INSERT INTO person_courses (person_id, course_id) VALUES ('$personId', '$courseId')";
$result = mysqli_query($connection, $sql);
Related Questions
- How can PHP developers ensure proper data filtering and validation when handling form submissions with radio buttons?
- What are the best practices for implementing a system that updates player accounts with money at regular intervals in a PHP application?
- What are the potential reasons for the menu and form not displaying correctly when using the include function in PHP?