How to add multiple records dynamically with php integration
Managing multiple inputs for data such as student records, teacher names, subjects and associated fees can be a complex task, especially when dynamic forms and database integrations are involved. The “add multiple records” approach simplifies this by enabling dynamic addition of fields and seamless data entry into the backend database. This tutorial guides you in implementing this efficient method using HTML, JavaScript, PHP, and MySQL.
Create database table
Before diving into form implementation, let’s set up the database table. This table will store multiple records including student name, email, phone, aadhaar number, teacher name, subject stream and total fees. Below is the SQL query to create login table in MySQL:
CREATE DATABASE newaddinputonclick;
USE newaddinputonclick;
CREATE TABLE login (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(100) NOT NULL,
aadhar VARCHAR(100) NOT NULL,
teacher VARCHAR(100) NOT NULL,
stream VARCHAR(100) NOT NULL,
total VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This table structure ensures efficient storage and retrieval of multi-record inputs.
Creating a Dynamic Input Form
Using HTML and JavaScript, we create a dynamic form where users can add multiple inputs for student details and associated fees. The following features are integrated into the form:
- Dynamic Input Addition: Users can add fields dynamically using the “Add Input” button.
- Field Validation: Required fields such as name, email and phone are valid.
- Stream Selection: Checkboxes allow users to select multiple streams (Hindi, English, Maths, Science) with respective fees.
- Global and Individual Totals: The total fees for each record are calculated, and a global total displays the cumulative amount.
Frontend Code
The HTML and JavaScript code handles the dynamic addition of input fields and calculates the fee based on the stream selected. Here is the required implementation:
Global Total: ₹0
The AddInput button dynamically creates new input rows, containing checkboxes for stream selection as well as fields for name, email, phone, and teacher name.
Backend Integration
Data from the form is processed and inserted into the database using PHP. Server-side code handles:
- Validation: Ensures all areas meet requirements.
- Batch Insertion: Uses SQL queries to insert multiple records efficiently.
- Error Handling: Catches database connection problems and query errors.
Here is the PHP code snippet to process the form data:
alert('Please select at least one stream.');";
continue;
}
$stream = mysqli_real_escape_string($conn, $studentStreams);
$total = mysqli_real_escape_string($conn, $totals[$i]);
$values[] = "('$name', '$email', '$phone', '$aadhar', '$teacher', '$stream', '$total')";
}
if (!empty($values)) {
$sql .= implode(", ", $values);
if (mysqli_query($conn, $sql)) {
header("Location: ball-slide-game.php");
exit();
} else {
echo "Error: " . mysqli_error($conn);
}
}
mysqli_close($conn);
}
?>
JavaScript code for dynamic input and fee calculation
- The JavaScript manages:
- Dynamically adding input fields.
- Validating input to ensure that all required fields are filled.
- Calculation of individual and global totals based on selected stream.
Here is the complete JavaScript code:
How It Works:
- Dynamic Inputs: The addNewInput function dynamically adds input fields to the form when the “Add Input” button is clicked.
- Checkbox Selection: Streams are represented as checkboxes, and their fees are calculated upon selection.
- Remove Input Rows: Each row has a “Remove” button to remove it from the form, the global total is recalculated accordingly.
- Global Total Update: The UpdateGlobalTotal function keeps track of the cumulative charges for all records.
Benefits of "Add input onclick" method
- Scalability: Easily accommodate any number of records without modifying the form structure.
- User-Friendly: Intuitive interface with minimal clicks to add new records.
- Efficiency: Batch processing reduces server load and ensures faster database interactions.
- Customizability: Adaptable to a variety of use cases like student registration, inventory management, and more.
Benefits of "Add input onclick" method
- Scalability: Easily accommodate any number of records without modifying the form structure.
- User-Friendly: Intuitive interface with minimal clicks to add new records.
- Efficiency: Batch processing reduces server load and ensures faster database interactions.
- Customizability: Adaptable to a variety of use cases like student registration, inventory management, and more.
Conclusion
The “Add input onclick” method provides a robust solution for dynamically adding and managing multi-record tables. Its integration with MySQL database ensures data consistency and reliability. By following this guide, you can streamline data entry processes and enhance the overall user experience.
Dynamic Input Form Download
Send download link to: