我无法使用PHP将表单数据保存到mysql数据库

时间:2015-06-18 10:56:18

标签: php mysql forms insert

我是PHP的新手,在这里遇到麻烦,我不知道为什么它不保存表单数据,如果你发现错误请帮助我。我已经尝试了很多编辑,但它根本不起作用。提前谢谢。

<?php  
session_start();  

if(!$_SESSION['username'])  
{  

    header("Location: index.php");//redirect to login page to secure the welcome page without login access.  
}

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Registeration Form</title>
<link rel="stylesheet" type="text/css" href="css/view.css" media="all">
<script type="text/javascript" src="js/view.js"></script>
<script type="text/JavaScript" src="js/forms.js"></script>

</head>
    <body id="main_body" >

        <img id="top" src="images/top.png" alt="">
        <div id="form_container">
            <h1>Registeration Form</h1>
            <div id="wrap">
            <!-- Here's all it takes to make this navigation bar. -->
               <ul id="nav">
                  <li><a href="manage.php">Home</a></li>
                  <li><a href="register.php">Register</a></li>
                  <li><a href="#">Edit</a></li>
                  <li><a href="#">View</a></li>
                  <li><a href="#">Delete</a></li>
                  <li><a href="logout.php">Logout</a></li>
               </ul>
            <!-- That's it! -->
            </div>

            <form action="<?=$_SERVER['PHP_SELF']?>" method="POST"  class="appnitro">
            <div class="form_description">
                <h2>Registeration Form</h2>
                <p>Please fill the form</p>
            </div>                      

                    <label class="description">Name </label>
                    <input name="name" class="element text medium" type="text" maxlength="255" value=""/>


                    <label class="description">Email </label>
                    <input name="email" class="element text medium" type="text" maxlength="255" value=""/> 


                    <label class="description" >Department </label>                 
                    <input name="department" class="element text medium" type="text" maxlength="255" value=""/> 


                    <label class="description">Respective Professor</label>
                    <input name="professor" class="element text medium" type="text" maxlength="255" value=""/> 


                    <label class="description">Position</label>
                    <select class="element select medium" name="position"> 
                        <option value="" selected="selected"></option>
                        <option value="Student-Bachelors" >Student-Bachelors</option>
                        <option value="Student-Masters" >Student-Masters</option>
                        <option value="Student-PhD" >Student-PhD</option>
                        <option value="Internee" >Internee</option>
                        <option value="Employee" >Employee</option>
                    </select>


                    <label class="description">Duration</label>
                    <span>
                    <input name= "duration_start" class="element text" maxlength="255" size="14" value=""/>
                    <label>Start</label>
                    </span>
                    <span>
                    <input name= "duration_end" class="element text" maxlength="255" size="14" value=""/>
                    <label>End</label>
                    </span> 

                    <label class="description">Room & Building</label>
                    <span>
                    <input  name= "room" class="element text" maxlength="255" size="14" value=""/>
                    <label>Room</label>
                    </span>
                    <span>
                    <input name= "building" class="element text" maxlength="255" size="14" value=""/>
                    <label>Building</label>
                    </span> 

                    <label class="description" >Address </label>
                    <input name="address" class="element text large" value="" type="text">


                    <label class="description" >Phone Number </label>   
                    <input name="phone_number" class="element text" maxlength="255" value="" type="text">

                    <input type="submit" name="submit" id="submit" value="Create my account" title="" border="0">

            </form> 

            <div id="footer"> Login as:
                <?php  
                    echo $_SESSION['username'];
                ?>, 

            </div>
        </div>
            <img id="bottom" src="images/bottom.png" alt="">
    </body>
</html>

<?php  

require_once("db_connect.php");//make connection here  
if(isset($_POST['submit']))  
{  
    $name=$_POST['name'];//here getting result from the post array after submitting the form.    
    $email=$_POST['email'];//same
    $department=$_POST['department'];//same 
    $professor=$_POST['professor'];//same
    $position=$_POST['position'];//same
    $duration_start=$_POST['duration_start'];//same
    $duration_end=$_POST['duration_end'];//same
    $room=$_POST['room'];//same
    $building=$_POST['building'];//same
    $address=$_POST['address'];//same
    $phone_number=$_POST['phone_number'];//same

    if($name=='')  
    {  
        //javascript use for input checking  
        echo"<script>alert('Please enter the name')</script>";  
    exit();//this use if first is not work then other will not show  
    }  

    if($email=='')  
    {  
        echo"<script>alert('Please enter the email')</script>";  
    exit();  
    }  

    if($department=='')  
    {  
        echo"<script>alert('Please enter the department')</script>";  
    exit();  
    }  

    if($professor=='')  
    {   
        echo"<script>alert('Please enter the professor')</script>";  
    exit();
    }  

    if($position=='')  
    {  
        echo"<script>alert('Please enter the position')</script>";  
    exit();  
    }  

    if($duration_start=='')  
    {  
        echo"<script>alert('Please enter the duration end')</script>";  
    exit();  
    }  
    if($duration_end=='')  
    {   
        echo"<script>alert('Please enter the duration start')</script>";  
    exit();
    }  

    if($room=='')  
    {  
        echo"<script>alert('Please enter the room')</script>";  
    exit();  
    }  

    if($building=='')  
    {  
        echo"<script>alert('Please enter the building')</script>";  
    exit();  
    }  
    if($address=='')  
    {  
        echo"<script>alert('Please enter the address')</script>";  
    exit();  
    }  

    if($phone_number=='')  
    {  
        echo"<script>alert('Please enter the phone number')</script>";  
    exit();  
    }  

    //here query check weather if user already registered so can't register again.  
    $check_email_query="select * from new_members WHERE email='$email'";  
    $run_query=mysqli_query($mysqli,$check_email_query);  

    if(mysqli_num_rows($run_query)>0)  
    {  
        echo "<script>alert('Email $email is already exist in our database, Please try another one!')</script>";  
        exit();  
    } 

    //insert the user into the database.  
    $insert_user="insert into new_members(name,email,professor,department,position,duration_start,duration_end,room,building,address,phone_number) VALUES ('$name','$email','$professor','$department','$position','$duration_start','$duration_end','$room','$building','$address','$phone_number')";  
    if(mysqli_query($mysqli,$insert_user))  
    {  
         header("Location:manage.php");
         exit();
        //echo "<script>window.open('manage.php','_self')</script>";  
     }
   }
?>

0 个答案:

没有答案