使用刷新页面将空白条目添加到sql表中

Here's the code.

<?php
    //get each input from form into a variable
        $firstname=$_POST['first_name'];
        $lastname=$_POST['last_name'];
        $gender=$_POST['gender'];
        $district=$_POST['district'];
        $email=$_POST['email'];
        $password=$_POST['password'];

        mysql_connect("localhost","root","");
        mysql_select_db("maindb");
        $result=mysql_query("SELECT email from user_info where email='$email' ");

        //checking customer already exists  
        if(mysql_num_rows($result)>0)
        {
            echo "Email Already exists. Please enter a different email"; 
        }

        //inserting customer details
        else
        {
            mysql_query("INSERT into user_info VALUES('$firstname','$lastname', NULL, '$gender','$district','$email','$password')");

            echo "New User Added Successfully!";
        }

            ?>

<form name="user" method="post" action="" onSubmit="return matchpasswords()">
            <caption><h2>Registration Form</h2></caption>
            <fieldset>
                <label>FirstName</label><br/>
                <input type="text" name="first_name" class="form-text" required>
            </fieldset>
            <fieldset>
                <label>LastName</label><br/>
                <input type="text" name="last_name" class="form-text" required>
            </fieldset>
            <fieldset class="radio">
                <label>Gender</label><br/>
                <input type="radio" name="gender" value="M"/>Male
                <input type="radio" name="gender" value="F"/>Female
            </fieldset>
            <fieldset>
                <label>District</label><br/>
                <select name="district"required>
                    <option selected>Colombo</option>
                    <option>Kandy</option>
                    <option>Matara</option>
                    <option>Galle</option>
                </select>
            </fieldset>
            <fieldset>
                <label>Email</label><br/>
                <input type="email" name="email" class="form-text" required>
            </fieldset>
            <fieldset>
                <label>Password</label><br/>
                <input type="password" name="password" class="form-text" required>
            </fieldset>
            <fieldset>
                <label>ConfirmPassword</label><br/>
                <input type="password" name="confirmation" class="form-text" required>
            </fieldset>
            <fieldset>
                <input type="submit" value="Submit"/>
            </fieldset>
            <fieldset>
                Already Signed Up? <a href="login.php"><h3>Login here.</h3></a>     
            </fieldset>
        </form>

I was suggested to add this part to the beginning

if(isset($_POST["submit"]))

but after addition of this, you cannot insert anything into the database. Without the isset part you can insert into the datatable but with each refresh of the page and on page load a blank entry is added to the datatable and I get several errors.

Notice: Undefined index: first_name in D:\xampp\htdocs\embrace\useregistration.php on line 85

for each field in the sql table.

Instead if(isset($_POST["submit"])) use if(!empty($_POST)).

So it doesent matter if an input with submit name is sent.

Or replace <input type="submit" value="Submit"/> with <input type="submit" name="submit" value="Submit"/>

"I was suggested to add this part to the beginning

if(isset($_POST["submit"]))

but after addition of this, you cannot insert anything into the database"

It means you are not getting the form data. Check every form field has "name" attribute.

why you want to check if(isset($_POST["submit"])).just give the action of your backend page to the form . You can do the validation by php or javascript your query will run according to your if condition

if you use this code "if(isset($_POST["submit"]))". you should replace you code as well

        <fieldset>
            <input type="submit" value="Submit"/>
        </fieldset>

chang code with this

        <fieldset>
            <input type="submit" name="submit" value="Submit"/>
        </fieldset>

hopefully it will make sense.