Insert Student Data into SQLite Using PHP PDO and Bootstrap FORM

HTML Form with Bootstrap

Insert Student Data


This HTML form is designed using Bootstrap for styling, making it responsive and easy to use. The form collects information like the student’s name, class, mark, and gender, using input fields and a dropdown. The form uses the POST method to submit the data to the PHP script insert_student.php for processing.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Insert Student Data</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        form {
            width: 50%;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h2>Insert Student Data</h2>
        <form method="POST" action="insert_student.php" class="form-group">
            <div class="mb-3">
                <label for="name" class="form-label">Student Name</label>
                <input type="text" name="name" id="name" class="form-control" required>
            </div>
            <div class="mb-3">
                <label for="class" class="form-label">Class</label>
                <input type="text" name="class" id="class" class="form-control" required>
            </div>
            <div class="mb-3">
                <label for="mark" class="form-label">Mark</label>
                <input type="number" name="mark" id="mark" class="form-control" required>
            </div>
            <div class="mb-3">
                <label for="gender" class="form-label">Gender</label><br>
                <input type="radio" name="gender" id="male" value="Male" required> 
                <label for="male">Male</label>
                <input type="radio" name="gender" id="female" value="Female" required> 
                <label for="female">Female</label>
                <input type="radio" name="gender" id="others" value="Others" required>
                <label for="others">Others</label>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>
</body>
</html>

PHP Script (insert_student.php)

The PHP script connects to the SQLite database using PDO. It validates the form input (ensuring all fields are filled and mark is numeric). If validation passes, it inserts the data into the student table. On success, it returns the newly added student ID; otherwise, it shows an error message.


$my_conn the connection object is taken from config.php file

require 'config.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $class = $_POST['class'];
    $mark = $_POST['mark'];
    $gender = $_POST['gender'];

    // Validation
    if (empty($name) || empty($class) || empty($mark) || empty($gender)) {
        echo "<div class='alert alert-danger'>All fields are required!</div>";
    } elseif (!is_numeric($mark)) {
        echo "<div class='alert alert-danger'>Mark must be a number!</div>";
    } else {
        try {
            $query = "INSERT INTO student (name, class, mark, gender) 
			  VALUES (:name, :class, :mark, :gender)";
            $stmt = $my_conn->prepare($query);
            $stmt->bindParam(':name', $name);
            $stmt->bindParam(':class', $class);
            $stmt->bindParam(':mark', $mark);
            $stmt->bindParam(':gender', $gender);

            if ($stmt->execute()) {
                echo "<div class='alert alert-success'>
		Data added successfully! Student ID: " . $my_conn->lastInsertId() . "</div>";
            }
        } catch (PDOException $e) {
            echo "<div class='alert alert-danger'>Error: " . $e->getMessage() . "</div>";
        }
    }
}

Podcast on SQLite database management using PHP PDO

PHP SQLite Insert record MySQL Blob data
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer