-3

I am making a form which will input details into a mysql database.

I have one problem. I have used the "required" attribute in my html form, but still everytime I load the page it gives an entry into the database (even though the fields are empty).

This is the code (php):

<!DOCTYPE html>
<?php
        $cnn = mysqli_connect("localhost", "root", "abc123", "usercake");

// Check connection
        if (mysqli_connect_errno($cnn)) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];

        $qry = "INSERT INTO usercake.huffaz (country) VALUES ('$_POST[country]')";
        mysqli_query($cnn, $qry)
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="huffaz.php" method="post" >
            <fieldset>
                <legend>
                    Your Personal Details
                </legend>
                <label>First Name:</label>
                <input type="text" name="firstname" required />
                <label>Surname:</label>
                <input type="text" name="surname" required />
                <label>Age at 1st Ramadhan:</label>
                <input type="number" name="age" min="15" required />
            </fieldset>
            <fieldset>
                <legend>
                    Your Contact Details
                </legend>
                <label>City:</label>
                <input type="text" name="city" required/>
                <label>County/State:</label>
                <input type="text" name="state" required/>
                <label>Country:</label>
                <input type="text" name="country" required/>
            </fieldset>
            <fieldset>
                <legend>
                    Your Qualifications
                </legend>
                <input type=""/>
            </fieldset>
            <input type="submit" />
        </form>
    </body>
</html>
4

1 に答える 1