index.php

 avatar
user_6042121
php
2 years ago
7.0 kB
18
Indexable
Never
<!DOCTYPE html>

<html>

<head>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>
        body {
            font-family: Arial, Helvetica, sans-serif;
            background-color: white;
        }

        * {
            box-sizing: border-box;
        }

        .container {
            padding: 16px;
            background-color: white;
        }

        input[type=text],
        input[type=password] {
            width: 100%;
            padding: 15px;
            margin: 5px 0 22px 0;
            display: inline-block;
            border: none;
            background: #EFF5F5;

        }

        input[type=text]:focus,
        input[type=password]:focus {
            background-color: #EFF5F5;
            outline: none;
        }

        hr {
            border: 1px solid #DC3535;
            margin-bottom: 25px;
        }

        .registerbtn {
            background-color: #497174;
            color: white;
            padding: 16px 20px;
            margin: 8px 0;
            border: none;
            cursor: pointer;
            width: 100%;
            opacity: 0.9;
        }

        .registerbtn:hover {
            opacity: 100;
        }

        .box-container {
            height: 400px;
            width: 600px;
            position: relative;
        }

        .hero {
            display: flex;
        }
    </style>

</head>

<body>

    <!-- Info.class -->

    <?php

    $lname = $fname = $mi = $age = $cn = $em = $ads = $pass = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $lname = test_input($_POST["lname"]);

        $fname = test_input($_POST["fname"]);

        $mi = test_input($_POST["mi"]);

        $age = test_input($_POST["age"]);

        $cn = test_input($_POST["cn"]);

        $em = test_input($_POST["em"]);

        $ads = test_input($_POST["ads"]);
    }



    function test_input($data)
    {

        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    ?>


    <div class="hero">

        <div class="box-container">

            <!-- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> -->
            <form action="details_entry.php" method="post">

                <div class="container">

                    <h1>Online Registration</h1>

                    <p>Please fill in this form to create an account.</p>

                    <hr>

                    <label for="lname"><b>Last Name</b></label>

                    <input type="text" placeholder="Enter Last Name" name="lname" id="lname" required>

                    <label for="fname"><b>First Name</b></label>

                    <input type="text" placeholder="Enter First Name" name="fname" id="fname" required>

                    <label for="mi"><b>Middle Initial</b></label>

                    <input type="text" placeholder="Enter Middle Initial" name="mi" id="mi" required>

                    <label for="age"><b>Age</b></label>

                    <input type="text" placeholder="Enter Age" name="age" id="age" required>

                    <label for="cn"><b>Contact No.</b></label>

                    <input type="text" placeholder="Enter Contact No." name="cn" id="cn" required>

                    <label for="em"><b>Email</b></label>

                    <input type="text" placeholder="Enter Email" name="email" id="em" required>

                    <label for="ads"><b>Address</b></label>

                    <input type="text" placeholder="Enter Address" name="ads" id="ads" required>

                    <button type="submit" class="registerbtn">Register</button>

                </div>

            </form>

        </div>




        <div class="table">

            <h1> Output </h1>


<?php
        // Create database connection
        $server_name = "localhost";
        $username = "root";
        $password = "";
        $database_name = "regform";
        
        // Create connection
        $conn = new mysqli($servername, $username, $password,  $database);
        
        // Check connection
        if(!$conn) {
            die("ERROR: Could not connect. " .mysqli_connect_error());
        }
        
        if (isset($_POST['lname']) || isset($_POST['fname']) || isset($_POST['mi']) || isset($_POST['age']) || isset($_POST['cn']) || isset($_POST['email']) || isset($_POST['ads'])) {
        $last_name =  $_POST['lname'];
        $first_name = $_POST['fname'];
        $middle_name =  $_POST['mi'];
        $age = $_POST['age'];
        $contact = $_POST['cn'];
        $email = $_POST['email'];
        $address = $_POST['ads'];
        
        if ($last_name != "" || $first_name != "" || $middle_name != "" || $age != "" || $contact != "" || $email != "" || $address != "")
        {
            // Performing insert query execution
            $sql = "INSERT INTO registration VALUES ('$last_name', '$first_name','$middle_name','$age','$contact', '$email', '$address')";
                 
            if(mysqli_query($conn, $sql)){
                echo "<h3><center>Database updated!</center><br><br></h3>";
            } else{
                echo "ERROR: Hush! Sorry $sql. " . mysqli_error($conn);
            }
                
            // Run the query to display the updated database table.
            $result = $conn->query("SELECT * FROM registration");
            
            // Get the result in to a more usable format.
            $query = array();
            while($query[] = mysqli_fetch_assoc($result));
            array_pop($query);
            
            // Output a dynamic table of the results with column headings.
            echo '<table border="2">';
            echo '<tr>';
            foreach($query[0] as $key => $value) {
                echo '<td>';
                echo $key;
                echo '</td>';
            }
            echo '</tr>';
            foreach($query as $row) {
                echo '<tr>';
                foreach($row as $column) {
                    echo '<td>';
                    echo $column;
                    echo '</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
        }

        else{             
            echo "<h5>No saved entry yet!</h5>";
            // Close connection
            mysqli_close($conn);
        }
    }
?>

        </div>

    </div>



    <style>
        table,
        th,
        td {

            border: 1px solid;
        }

        .table {
            margin-top: 30px;
            margin-left: 80px;
            height: 400px;
            width: 600px;

        }
    </style>

</body>

</html>