避免重复输入和缓存输入的表单值

时间:2014-11-05 09:20:28

标签: php html mysql

我有一些控制重复条目的代码,特别是USER ID。它在提交时检查数据库,如果该用户ID已经存在则会提供该通知。现在问题是当我提交并且如果输入的用户ID已经存在于数据库中时,表单上的所有其他条目都被清除,提示我再次重新输入所有其他详细信息。我觉得这很烦人,倒退了。我想要一些帮助,我可以做得更好,只有USER ID文本框返回空,保持其他细节安全/不变,或者确实保持/缓冲/缓存先前输入的所有细节,以便我也可以查看重复的USER ID在改变它之前。

new_user.php

        <h1 align="center">Create New User</h1>
        <p align="center" style="color:#F00"><?php if(isset($_GET['dup'])){ echo "That User ID Already Exists!"; } ?>&nbsp;</p>
           <form id="form1" method="post" action="add_user.php">
                <table width="100%">
                    <tr>
                <td width="204"><div align="right">User ID:</div></td>
                <td width="286">
                  <input type="text" name="user_id" id="user_id" />
               </td>
                  </tr>
                    <tr>
                      <td><div align="right">Full Names:</div></td>
                      <td>
                        <input type="text" name="fname" id="fname" />
                      </td>
                  </tr>
                    <tr>
                      <td><div align="right">Gender:</div></td>
                      <td><select id="sex" name="sex">
                      <option selected="selected" value="male">Male</option>
                      <option name="female">Female</option>
                      </select></td>
                  </tr>
                    <tr>
                      <td><div align="right">NRC Number:</div></td>
                      <td>
                      <input type="number" name="nrcno" id="nrcno" min="1"  />
                     </td>
                  </tr>
                    <tr>
                      <td><div align="right">Phone Number:</div></td>
                      <td>
                      <input type="number" name="cellno" id="cellno" />
                     </td>
                  </tr>
                    <tr>
                      <td><div align="right">Email Address:</div></td>
                      <td>
                      <input type="email" name="emailad" id="emailad" />
                      </td>
                  </tr>
                    <tr>
                      <td><div align="right">Position Held:</div></td>
                      <td>
                        <input type="text" name="posh" id="posh" />
                      </td>
                  </tr>
                    <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="create" id="create" value="Add User" /></td>
                    </tr>
                </table>
          </form>

add_user.php

        <?php
        $user_id=$_POST['user_id'];
        $fname = $_POST['fname'];
        $sex= $_POST['sex'];
        $name= $_POST['name'];
        $nrcno = $_POST['nrcno'];
        $cellno= $_POST['cellno'];
        $emailad = $_POST['emailad'];
        $posh = $_POST['posh'];


        require("get_func.php");

        checkID($id);

        include("connect.php");

        mysql_select_db("ceec", $con);

        $query = "INSERT INTO user VALUES ('$user_id', '$fname', '$sex','$name', '$nrcno', '$cellno', '$emailad', '$posh')";

        if (mysql_query($query)){

            header("Location: success.php");

            }
        else {echo "Nada" . mysql_error(); }
        mysql_close($con);
                   ?> 

get_func.php

         <?php                      
        function checkID($id){

        include_once("connect.php");

        mysql_select_db("ceec",$con);

        $query = "SELECT * FROM user WHERE user_id = '$id'";

        $result= mysql_query($query);   

        if($row = mysql_fetch_array($result))

        {
        header("Location: new_user.php?dup=true");
        break;  
        }

        else {}

        }
        ?>

1 个答案:

答案 0 :(得分:0)

<input type="text" name="user_id" id="user_id" 
<?php if(isset($_POST['user_id'])){echo htmlentities($_POST['user_id'];} ?>/>