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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件