2016-09-08 143 views
-1

我是新來的PHP,我試圖註冊用戶代碼,但它不能正常工作。如果我註釋掉頭('location:login.php');然後它將數據保存在數據庫中,如果我取消註釋此頭('location:login.php');那麼它不會將數據保存在數據庫中並打開登錄頁面。php註冊表問題

<html> 
    <head> 
     <title>records...</title> 
    </head> 
    <body> 
     <form action="registration.php" method="post"> 
      <table border="1" align="center" cellspacing="2"> 
       <tr> 
        <th>ID</th> 
        <td><input type="text" name="id"></td> 
       </tr> 
       <tr> 
        <th>username</th> 
        <td><input type="text" name="username"></td> 
       </tr> 
       <tr> 
        <th>password</th> 
        <td><input type="password" name="password"></td> 
       </tr> 
       <tr> 
        <th>email</th> 
        <td><input type="text" name="email"></td> 
       </tr> 
       <tr> 
        <th>contact</th> 
        <td><input type="text" name="contact"></td> 
       </tr> 
       <tr> 
        <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td> 
       </tr> 
      </table> 
     </form> 
     <?php 
      error_reporting(0); 

      if(isset($_REQUEST["submit"])){ 
       //header('location:login.php'); 

       require 'conn.php'; 
       $id=$_REQUEST['id']; 
       $username=$_REQUEST['username']; 
       $password=$_REQUEST['password']; 
       $email=$_REQUEST['email']; 
       $contact=$_REQUEST['contact']; 
       $query="insert into dhr values('$id','$username','$password','$email','$contact')"; 

       $result= mysql_query($query); 
       if($result>0){ 
        echo"insert successfully.."; 
       } else { 
        echo "not inserted". mysql_error(); 
       } 
      } 
     ?> 
    </body> 
</html> 
+0

能否請您給我解釋一下您的具體問題? –

+0

您能否指定錯誤或問題? – Dave

+0

...你寫了這段代碼嗎?我非常懷疑它,如果你這樣做,那麼請不要在不知道它做什麼的情況下添加代碼,'header(location:...);'重定向用戶,所以當然如果你不發表評論,它會重定向用戶,因爲它應該是 – Epodax

回答

0
you not to be define id in this form.because ID should be autoincrement. then after you create a login page(login.php) then two fields username and password same as just like work on registration page. 


<html> 
    <head> 
     <title>records...</title> 
    </head> 
    <body> 
     <form action="" method="post"> 
      <table border="1" align="center" cellspacing="2"> 

       <tr> 
        <th>username</th> 
        <td><input type="text" name="username"></td> 
       </tr> 
       <tr> 
        <th>password</th> 
        <td><input type="password" name="password"></td> 
       </tr> 
       <tr> 
        <th>email</th> 
        <td><input type="text" name="email"></td> 
       </tr> 
       <tr> 
        <th>contact</th> 
        <td><input type="text" name="contact"></td> 
       </tr> 
       <tr> 
        <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td> 
       </tr> 
      </table> 
     </form> 
     <?php 
      if(isset($_REQUEST["submit"])){ 
       require 'conn.php'; 
       $username=$_REQUEST['username']; 
       $password=$_REQUEST['password']; 
       $email=$_REQUEST['email']; 
       $contact=$_REQUEST['contact']; 
       $query="insert into dhr(username,password,email,contact)values('$id','$username','$password','$email','$contact')"; 

       $result= mysql_query($query); 
       if($result>0){ 
        // echo"insert successfully.."; 
         header('location:login.php'); 
       } else { 
        echo "not inserted". mysql_error(); 
       } 
      } 
     ?> 
    </body> 
</html> 
0

這是因爲你正在檢查form後張貼在重定向或不

if(isset($_REQUEST["submit"])){ 


       header('location:login.php');// this will redirect you without executing your code below this Beacuse you have written it here 

就在保存值之前寫的insert query 下面像

if($result>0){ 

       echo"insert successfully.."; 
       header('location:login.php');// if you want redirect from here 
      } 
1

您將會重定向DATABSE。將值保存到數據庫後使用header()函數。這樣做。這裏是你的完整正確的代碼。

<?php 
    error_reporting(0); 
    if(isset($_REQUEST["submit"])){ 
     require 'conn.php'; 
     $id = $_REQUEST['id']; 
     $username = $_REQUEST['username']; 
     $password = $_REQUEST['password']; 
     $email = $_REQUEST['email']; 
     $contact = $_REQUEST['contact']; 
     $query = "insert into dhr values('$id','$username','$password','$email','$contact')";    
     if(mysql_query($query)){ 
      //echo"insert successfully.."; 
      header('location:login.php'); 
     }else{ 
      echo "not inserted". mysql_error(); 
     } 
    } 
?> 
<html> 
    <head> 
     <title>records...</title> 
    </head> 
    <body> 
     <form action="registration.php" method="post"> 
      <table border="1" align="center" cellspacing="2"> 
       <tr> 
        <th>ID</th> 
        <td><input type="text" name="id"></td> 
       </tr> 
        <tr> 
        <th>username</th> 
        <td><input type="text" name="username"></td> 
       </tr> 
       <tr> 
        <th>password</th> 
        <td><input type="password" name="password"></td> 
       </tr> 
       <tr> 
        <th>email</th> 
        <td><input type="text" name="email"></td> 
       </tr> 
       <tr> 
        <th>contact</th> 
        <td><input type="text" name="contact"></td> 
       </tr> 
       <tr> 
        <td colspan="2" align="center" style="color: red"><input type="submit" name="submit" value="submit"></td> 
       </tr> 


      </table> 
     </form> 

    </body> 
</html> 

記住,標題()必須被調用之前的任何實際的輸出是 發送,或者通過正常的HTML標記,空行的文件,或者從PHP。 click here for more details about header function

+0

很好的答案。請注意,對於INSERT查詢,mysql_query()返回true或false:對於其他類型的SQL語句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功時返回TRUE,在FALSE時返回FALSE錯誤「。建議編輯。 http://php.net/manual/en/function.mysql-query.php – Narxx

+0

@Narxx,是的,你是絕對正確的。我們也可以使用'mysql_insert_id' - 獲取在最後一次查詢中生成的ID,知道是否將該記錄插入到數據庫中。 –