Monday, 9 December 2024

php program

 <html>

<head>

    <title>Form Data to Database</title>

</head>

<body>

    <h2>Input Form</h2>

    <form action="" method="POST">

        <table border="0" cellpadding="10" cellspacing="0">

            <tr>

                <td><label for="name">Name:</label></td>

                <td><input type="text" id="name" name="name" required></td>

            </tr>

            <tr>

                <td><label for="father_name">Father's Name:</label></td>

                <td><input type="text" id="father_name" name="father_name" required></td>

            </tr>

            <tr>

                <td><label for="email">Email:</label></td>

                <td><input type="email" id="email" name="email" required></td>

            </tr>

            <tr>

                <td><label for="phone">Phone:</label></td>

                <td><input type="tel" id="phone" name="phone" pattern="[0-9]{10}" required></td>

            </tr>

            <tr>

                <td><label for="address">Address:</label></td>

                <td><textarea id="address" name="address" rows="4" cols="30" required></textarea></td>

            </tr>

            <tr>

                <td><label for="dob">Date of Birth:</label></td>

                <td><input type="date" id="dob" name="dob" required></td>

            </tr>

            <tr>

                <td><label for="gender">Gender:</label></td>

                <td>

                    <select id="gender" name="gender" required>

                        <option value="Male">Male</option>

                        <option value="Female">Female</option>

                        <option value="Other">Other</option>

                    </select>

                </td>

            </tr>

            <tr>

                <td colspan="2" style="text-align: center;">

                    <button type="submit" name="submit">Submit</button>

                </td>

            </tr>

        </table>

    </form>

    <?php

    // Handle form submission

    if (isset($_POST['submit'])) {

        

// Database configuration

        $servername = "localhost";

        $username = "root"; // Default MySQL username

        $password = ""; // Default password

        $dbname = "form_data";


        // Connect to MySQL

        $conn = new mysqli($servername, $username, $password, $dbname);

// $conn = new mysqli("localhost", "root", "", "form_data");


        // Check connection

        if ($conn->connect_error) 

{

            die("Connection failed: " . $conn->connect_error);

        }


        // Retrieve form data

        $name = htmlspecialchars($_POST['name']);

        $father_name = htmlspecialchars($_POST['father_name']);

        $email = htmlspecialchars($_POST['email']);

        $phone = htmlspecialchars($_POST['phone']);

        $address = htmlspecialchars($_POST['address']);

        $dob = htmlspecialchars($_POST['dob']);

        $gender = htmlspecialchars($_POST['gender']);


        // Insert data into database

        $sql = "INSERT INTO user_data (name, father_name, email, phone, address, dob, gender)

                VALUES ('$name', '$father_name', '$email', '$phone', '$address', '$dob', '$gender')";

//if-else for connection data sucessfully saved or not

        if ($conn->query($sql) === TRUE) {

            echo "<p>Data successfully saved to the database!</p>";

        } else {

            echo "<p>Error: " . $sql . "<br>" . $conn->error . "</p>";

        }


        // Close connection

        $conn->close();

    }

    ?>

</body>

</html>

No comments:

Post a Comment