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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题