dronthpi05943 2015-07-31 10:29
浏览 51
已采纳

使用刷新页面将空白条目添加到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.

  • 写回答

4条回答 默认 最新

  • du248227 2015-07-31 10:48
    关注

    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"/>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?