-3

我正在制作一个表格,将详细信息输入到 mysql 数据库中。

我有一个问题。我在我的 html 表单中使用了“必需”属性,但每次我加载页面时,它都会在数据库中输入一个条目(即使字段为空)。

这是代码(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

1

您没有检查是否有任何职位设置/解雇。

诚哥是这样的:

if(isset($_POST)){
    $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)
} 
于 2013-06-26T12:13:42.753 回答