在php中刷新页面时,数据被插入到数据库中

时间:2016-02-16 12:02:57

标签: php mysql

这是我项目的emp_registration页面。进行刷新,它将数据插入数据库。

这是我的代码:

  <?php        
    session_start();
    if(!isset($_SESSION['username']))
    {
        header('location:Admin.php');
    }

    ?>   

这是我的会话变量

这低于我的html代码表格进行注册

    <html>        
        <head>          
            <title> Employee | Registration</title>             
            <meta name="viewport" content="device=device-width , initial-scale=1.0"> 

            <!-- Main Css bootstrap -->
            <link rel="stylesheet" href="../Components/css/bootstrap.min.css" type="text/css">

            <!-- Jquery-ui Plugin Css -->
            <link rel="stylesheet" href="../Components/css/jquery-ui.css" type="text/css">

            <!-- font aweosome icon set css-->
            <link rel="stylesheet" href="../Components/css/font-awesome.min.css" type="text/css">

            <!-- animate css -->
            <link rel="stylesheet" href="../Components/css/animate.css" type="text/css">    


            <!-- Main Jquery -->
            <script src="../Components/js/jquery_main.js"></script>

            <!-- Main Bootstrap -->
            <script src="../Components/js/bootstrap.min.js"></script>

            <!-- Jquery-ui Plugin -->
            <script src="../Components/js/jquery-ui.js"></script>

            <!-- Custom js plugin for animate onClick -->
            <script src="../Components/js/custom_js.js"></script>


            <style>

                body{

                    background-image:url("../Components/images/admin_panel.png");
                    background-size:cover;
                    background-repeat:no-repeat;
                }
                a:hover{
                    text-decoration:none;
                }
                .navbar-brand
                {
                        position: absolute;
                        margin-bottom:100px;
                        width: 100%;
                        left: 0;
                        text-align: center;
                        margin: auto;
                }
                .opaque-navbar{
                    background-color:rgba(0,0,0,0);
                    transition: background-color .5s ease 0s;
                    border: 0px;
                }
                .form-horizontal{
                    max-width:443px;
                    margin:0 auto;
                }

                #imagePreview {
                    min-width:250px;
                    height:200px;
                    margin:0 auto;
                    background-position: center center;
                    background-size: cover;
                    -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.3);
                    display: inline-block;
                    }
                    .btn-width{
                        min-width:500px;
                        margin:0 auto;
                    }
                    .error{
                        color:#fff;
                        margin:0 auto;
                        margin-left:30%;
                    }


            </style>

            <script language="JavaScript" type="application/javascript">
                $(document).ready(function(){
                $(".error").hide(); });

            function validate()
                {

                     if(document.regForm.fname.value == "")
                     {
                         $("#fname").before("<span class='error'>Please Enter First Name ! </span>");
                         document.regForm.fname.focus();
                         return false;
                     }

                      if(document.regForm.lname.value == "")
                     {
                         $("#lname").before("<span class='error'>Please Enter Last Name ! </span>");
                         document.regForm.lname.focus();
                         return false;
                     }

                       var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                     if(document.regForm.username.value.trim() == "" || !re.test(document.regForm.username.value))
                    {
                        $("#username").before("<span class='error'>Please Provide User Id As A Email Address ! </span>");
                        document.regForm.username.value="";
                        document.regForm.username.focus();
                        return false;
                    }

                    if(document.regForm.password.value.trim() == "")
                    {
                        $("#password").before("<span class='error'>Please Provide Password !</span>");
                        document.regForm.password.focus();
                        return false;
                    }

                        var phoneno = /^\d{10}$/; 
                     if(document.regForm.mobile_number.value.trim()=="" || !phoneno.test(document.regForm.mobile_number.value))
                    {
                        $("#mobile_number").before("<span class='error'>Please Provide Mobile Number !</span>");
                        document.regForm.mobile_number.focus();
                        return false;
                    }

                     if(document.regForm.gender.value.trim() == "")
                    {
                        $("#gender").before("<span class='error'>Please Select Gender !</span>");
                        return false;
                    }

                      if(document.regForm.gender.value.trim() == "")
                    {
                        $("#gender").before("<span class='error'>Please Select Empoyee Type !</span>");
                        return false;
                    }

                     if(document.regForm.birth.value.trim() == "")
                    {
                        $("#birth").before("<span class='error'>Please Select Birth Date !</span>");
                        document.regForm.birth.focus();
                        return false;
                    }

                     if(document.regForm.joining.value.trim() == "")
                    {
                        $("#joining").before("<span class='error'>Please Select Joining Date !</span>");
                        document.regForm.joining.focus();
                        return false;
                    }

                     if(document.regForm.address.value.trim() == "")
                    {
                        $("#address").before("<span class='error'>Please Provide Permanent Address !</span>");
                        document.regForm.address.focus();
                        return false;
                    }

                    if(document.regForm.salary.value.trim() == "")
                    {
                        $("#salary").before("<span class='error'>Please Provide Basic Salary !</span>");
                        document.regForm.salary.focus();
                        return false;
                    }

                     if(document.regForm.state.value.trim() == "")
                    {
                        $("#state").before("<span class='error'>Please Select State !</span>");
                        document.regForm.state.focus();
                        return false;
                    }

                    if(document.regForm.city.value.trim() == "")
                    {
                        $("#city").before("<span class='error'>Please Select City !</span>");
                        document.regForm.city.focus();
                        return false;
                    }

                    if(document.regForm.designation.value.trim() == "")
                    {
                        $("#designation").before("<span class='error'>Please Select Designation !</span>");
                        document.regForm.designation.focus();
                        return false;
                    }

                    if(document.regForm.experaince.value.trim() == "")
                    {
                        $("#experaince").before("<span class='error'>Please Select Experiance!</span>");
                        document.regForm.experaince.focus();
                        return false;
                    }



            }



            </script><!-- end of validation of registration form -->


            <script>
                  $(function() {
                    $( "#joining" ).datepicker();
                  });
            </script><!-- end of datepicker -->

            <script>
                  $(function() {
                    $( "#birth" ).datepicker();
                  });
            </script><!-- end of birth datepicker -->

            <script>

                    function display(s1,s2){
                        var s1 = document.getElementById(s1);
                        var s2 = document.getElementById(s2);
                        s2.innerHTML = "";
                        if(s1.value == "Maharashtra"){
                            var optionArray = ["Pune|Pune","Mumbai|Mumbai","Nagpur|Nagpur","Nashik|Nashik","Solapur|Solapur","Thane|Thane","Aurangabad|Aurangabad"];
                        } else if(s1.value == "Gujrat"){
                            var optionArray = ["Ahmedabad|Ahmedabad","Surat|Surat","Vadodara|Vadodara","Rajkot|Rajkot","Bhavnagar|Bhavnagar","Jamnagar|Jamnagar","Gandhinagar|Gandhinagar"];
                        } else if(s1.value == "Madhya Pradesh"){
                            var optionArray = ["Indore|Indore","Bhopal|Bhopal","Jabalpur|Jabalpur","Gwalior|Gwalior","Ujjain|Ujjain","Sagar|Sagar","Dewas|Dewas"];
                        }else if(s1.value == "Delhi"){
                            var optionArray = ["New Delgi|New Delhi","Burari|Burari","Karawal Nagar|Karawal Nagar","Mustafabad|Mustafabad","Gokal Pur|Gokal Pur","Mandoli|Mandoli","Dallo Pura","Dallo Pura"];
                        }else if(s1.value == "Punjab"){
                            var optionArray = ["Ludhiana|Ludhiana","Amritsar|Amritsar","Jalandhar|Jalandhar","Patiala|Patiala","Bathinda|Bathinda","Ajitgarh|Ajitgarh","Hoshiarpur|Hoshiarpur"];
                        }else if(s1.value == "Andhra pradesh"){
                            var optionArray = [,"Visakhapatnam|Visakhapatnam","Vijayawada|Vijayawada","Guntur|Guntur","Nellore|Nellore","Kurnool|Kurnool","Kadapa|Kadapa","Kakinada|Kakinada"];
                        }else if(s1.value == "karnataka"){
                            var optionArray = ["Vijayapura|Vijayapura","Bengaluru|Bengaluru","Ballari|Ballari","Chamarajanagar|Chamarajanagar","Mangaluru|Mangaluru","Mysuru|Mysuru","Udupi|Udupi","Chennai|Chennai"];
                        }
                        for(var option in optionArray){
                            var pair = optionArray[option].split("|");
                            var newOption = document.createElement("option");
                            newOption.value = pair[0];
                            newOption.innerHTML = pair[1];
                            s2.options.add(newOption);
                        }
                    }

            </script><!-- end of state and city selector -->

            <script type="text/javascript">
                    $(function() {
                        $("#uploadFile").on("change", function()
                        {
                            var files = !!this.files ? this.files : [];
                            if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

                            if (/^image/.test( files[0].type)){ // only image file
                                var reader = new FileReader(); // instance of the FileReader
                                reader.readAsDataURL(files[0]); // read the local file

                                reader.onloadend = function(){ // set image data as background of div
                                    $("#imagePreview").css("background-image", "url("+this.result+")");
                                }
                            }
                        });
                    });
            </script><!-- end of file picker image -->

        </head>

            <body>


                <div class="container">

                    <header>

                        <div class="container">

                                <marquee><center> <a href="admin_panel.php"><h2> Welcome To Tej It Solutions </h2></a></center></marquee>

                        </div><!-- end of container in header -->

                    </header> <!-- end of header -->

                    <nav class="navbar navbar-default opaque-navbar row">

                            <div class="container">

                                               <div class="navbar-header">

                                                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#collapse">

                                                            <span class="icon-bar"></span>

                                                            <span class="icon-bar"></span>

                                                        <span class="icon-bar"></span>

                                                    </button>

                                                </div>

                                    <div class="navbar-collapse collapse" id="collapse">

                                        <a class="navbar-brand animated bounceInDown" style="color:red;"><big> Welcome <?php echo $_SESSION['username']; ?></big> </a>
                                            <br>
                                            <ul class="nav navbar-nav navbar-left">

                                                    <li><a href="admin_panel.php" class="animated fadeInLeft" style="color:white;"><i class="fa fa-home fa-3x"></i></a></li>

                                            </ul>

                                            <ul class="nav navbar-nav navbar-right">

                                                    <li><a href="logout.php" class="animated fadeInRight" style="color:white;"><i class="fa fa-user-times fa-3x"></i></a></li>

                                            </ul>

                                    </div>

                            </div><!-- End of container in nav -->


                    </nav> <!-- end of nav -->

                    <div class="content">

                            <div class="container">


                                <form  name="regForm" class="form-horizontal"  autocomplete="off" method="POST"  enctype="multipart/form-data" onsubmit="return validate();"    >

                                        <h2 style="color:#00BFFF; font-weight: bold" class="text-center"> Employee Registration  </h2><br>

                                <div class="form-group">

                                    <label class="control-label sr-only" for="fname" > First Name  </label>

                                    <div class="col-md-12 col-sm-12">

                                        <input  style="font-weight: bold;" name="fname"  id="fname" type="text" placeholder="Kindly Enter Employee First Name" class="form-control text-center"/>

                                    </div>

                                </div><!-- End of first name -->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="lname" > Last Name  </label>

                                    <div class="col-md-12 col-sm-12">

                                        <input style="font-weight: bold;" name="lname" id="lname" type="text" placeholder="Kindly Enter Employee Last Name" class="form-control text-center">

                                    </div>

                                </div><!-- end of last name -->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="username" > E-mail | UserName  </label>

                                    <div class="col-md-12 col-sm-12">

                                        <input style="font-weight: bold;" name="username" id="username" type="text" placeholder="Please Provide your Username as a E-mail " class="form-control text-center">

                                    </div>

                                </div><!-- end of username | E-mail -->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="password" > password  </label>

                                    <div class="col-md-12 col-sm-12 ">

                                        <input style="font-weight: bold;" name="password" id="password" type="password" placeholder="Kindly Enter Password" class="form-control text-center">

                                    </div>

                                </div><!-- end of Password-->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="mobile_numebr" > Mobile_Number </label>

                                    <div class="col-md-12 col-sm-12">

                                        <input style="font-weight: bold;" name="mobile_number" id="mobile_number" type="text" placeholder="Kindly Enter Mobile Number " class="form-control text-center">

                                    </div>

                                </div><!-- end of Mobile_Number-->

                                <div class="radio">

                                        <label class="col-md-6 col-sm-6 " style="color:red; font-weight:bold;"><input type="radio" name="gender" id="male" value="male" checked="true"/>Male</label>

                                        <label class="col-md-6 col-sm-6" style="color:red; font-weight:bold;"><input type="radio" name="gender" id="female" value="female"/>Fe-male</label>

                                </div><br><!-- end of gender -->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="address" > Address  </label>

                                    <div class=" col-md-12 col-sm-12">

                                        <textarea style="font-weight: bold;"rows="4" cols="50" name="address" id="address" type="text" placeholder="Kindly Enter Employee Address " class="form-control text-center"></textarea>

                                    </div>

                                </div><!-- end of address-->

                                <div class="radio">

                                        <label class="col-md-6 col-sm-6 " style="color:red; font-weight:bold;"><input type="radio" name="emptype" id="emptype"  value="employee"checked="true" />Employee</label>

                                        <label class="col-md-6 col-sm-6" style="color:red; font-weight:bold;"><input type="radio" name="emptype" id="emptype" value="trainee" />Trainee</label>

                                </div><br><!--end of type of employee -->

                                <div class="form-group">

                                    <label class="col-md-12 col-sm-12"> <input style="font-weight: bold;" type="text" id="birth" name="birth" class="form-control text-center" placeholder="Enter Birth-date of Employee">
                                    </label>
                                </div> <!-- end of Birth datepicekr -->

                                <div class="form-group">

                                    <label class="control-label sr-only" for="salary" > Basic Salary  </label>

                                    <div class="col-md-12 col-sm-12">

                                        <input style="font-weight: bold;" name="salary" id="salary" type="text" placeholder="Kindly Enter Employee Basic Salary" class="form-control text-center">

                                    </div>

                                </div><!-- end of Basic Salary -->

                                <div class="form-group">

                                    <label class="col-md-12 col-sm-12"><input style="font-weight: bold;" type="text" id="joining" name="joining" class="form-control text-center" placeholder="Enter Joining Date of Employee">
                                    </label>

                                </div> <!-- end of joining datepicekr -->



                                <div class="form-group">

                                      <label for="state" class="sr-only control-label">Select State:</label>

                                      <select style="font-weight: bold;" class="col-md-12 col-sm-12 form-control " id="state" name="state" onchange="display(this.id,'city')">
                                        <option value="" selected="selected">-- Select state --</option>
                                        <option>Maharashtra</option>
                                        <option>Gujrat</option>
                                        <option>Madhya Pradesh</option>
                                        <option>Delhi</option>
                                        <option>Punjab</option>
                                        <option>Andhra pradesh</option>
                                        <option>karnataka</option>

                                      </select>

                                </div><!-- end of state selctor -->

                                <div class="form-group">

                                    <label class="sr-only control-label">-- Select City --</label>

                                    <select style="font-weight:bold;" class="col-md-12 col-sm-12 form-control " id="city" name="city"></select>

                                </div><!-- end of city selector -->

                                <div class="form-group">

                                      <label for="designation" class="sr-only control-label">Select Designation Of Employee : </label>

                                      <select style="font-weight: bold;" class="col-md-12 col-sm-12 form-control" id="designation" name="designation">

                                        <option value="designation" selected="selected">-- Select Designation Of Employee --</option>

                                        <option>HR</option>
                                        <option>PHP developer</option>
                                        <option>Tester</option>
                                        <option>Marketing</option>
                                        <option>Trainer</option>
                                        <option>Project Manager</option>
                                        <option>Team lead</option>
                                        <option>JAVA Developer</option>
                                        <option>.NET Developer</option>
                                        <option>Android Developers</option>
                                        <option>Graphics Designer</option>


                                      </select>

                                </div><!-- end of Designation of employee selctor -->



                                <div class="form-group">

                                      <label for="experiance" class="sr-only control-label">Select Experiance Of Employee : </label>

                                      <select style="font-weight: bold;" class="col-md-12 col-sm-12 form-control" id="experaince" name="experiance">

                                            <option value="" selected="selected">-- Select Experiance Of Employee --</option>

                                                <option> Fresher    </option>
                                                <option> 1-2 Years  </option>
                                                <option> 2-3 years  </option>
                                                <option> 3-4 Years  </option>
                                                <option> 4-5 Years  </option>
                                                <option> 5-10 Years </option>
                                                <option> 10-20 Years </option>

                                    </select>

                                </div><!-- end of Employee Experiance selctor -->

                                <div class="form-group">

                                    <label class="sr-only control-label"> Submit Button </label>

                                    <input  type="submit" name="submit" value="submit" class="btn btn-danger btn-width form-control"/>

                                </div><!-- end of submit -->

                            </form><!-- end of form -->

                        </div><!-- end of container -->

                    </div><!-- end of Menu content -->

                </div> <!-- End of wrap -->

            </body> <!-- end of body -->

    </html>


    <!-- inserting data into database -->

这是我的php插入数据库代码

    <?php


            $dbc=mysqli_connect('localhost','admin','thepassword1','office') OR die('something wrong with your Database'.mysqli_connect_error());
            //connection is created     

            if (isset($_POST['submit']))
            {
                $fname=$_POST['fname'];
                $lname=$_POST['lname'];
                $mail=$_POST['username'];
                $password=SHA1($_POST['password']);
                $salary=$_POST['mobile_number'];
                $gender=$_POST['gender'];
                $address=$_POST['address'];
                $type=$_POST['emptype'];
                $birth_date=$_POST['birth'];
                $salary=$_POST['salary'];
                $joining_date=$_POST['joining'];
                $state=$_POST['state'];
                $city=$_POST['city'];
                $designation=$_POST['designation'];
                $experiance=$_POST['experiance'];

                $qry="INSERT INTO emp(emp_fname, emp_lname,emp_mail,emp_password,emp_number,emp_gender,emp_address,emp_type,emp_birth_date,emp_salary,emp_joining_date,emp_state,emp_city,emp_designation,emp_experiance) VALUES ('$fname','$lname','$mail','$password','$salary','$gender','$address','$type','$birth_date','$salary','$joining_date','$state','$city','$designation','$experiance')";
            //ending of query insert
            header("Location:admin_panel.php");
            if(mysqli_query($dbc, $qry))
            {
                echo"<script>alert('New Record Added succesfully')</script>";

            }
            else {
                echo "<script> alert('Error occur')</script>".mysqli_error($dbc);
            }
            }
                // end of decalartion of variable 

            mysqli_close($dbc);
            //sql connection is close


    ?>

3 个答案:

答案 0 :(得分:0)

我建议你使用html&lt; meta http-equiv =&#34; refresh&#34; conten =&#34; URL = ...&#34; /&GT;而不是php header()

答案 1 :(得分:0)

将用户标题为新页面:

在php脚本的末尾添加

ProjectId   Name    Budget  BC  Actual Spend    ETC         
"1      "   DeadRock    500,000 28,000  75,000  397,000 Budget-(BC+ActualSpend)     
"2      "   HardRock    300,000 "   33,000" 85,000  182,000 Budget-(BC+ActualSpend)     

通过这样做,刷新的用户将刷新header("location: newpage.php"); exit; ,这意味着它不会进行两次插入。

最佳建议:如果不插入,请先检查用户是否存在!

答案 2 :(得分:0)

重定向代码应在成功插入后。请试试这个:

if(mysqli_query($dbc, $qry))
{
    echo"<script>alert('New Record Added successfully')</script>"; 
    header("Location:admin_panel.php");
    exit;
}