2017-04-06 64 views
0

我基本上有兩種形式在一個頁面。 第一個用於登錄,第二個用於插入數據。 二表單操作正常。我可以用它插入數據。 但我用於登錄用戶的相同形式,但它不起作用。 點擊提交按鈕後,什麼也沒有發生,只是頁面刷新。表單操作不工作在PHP?

請參閱我的代碼,並幫助我解決我的第一個表單的操作問題。

<div class="login_wrapper"> 
    <div id="login" class="animate form login_form"> 
    <section class="login_content"> 
     <form action="login.php" method="post"> 
     <h1>Login Form</h1> 
     <div> 
      <input type="text" class="form-control" placeholder="Username" required="" name="username" /> 
     </div> 
     <div> 
      <input type="password" class="form-control" placeholder="Password" required="" name="password" /> 
     </div> 
     <div> 
      <input type="submit" class="btn btn-default" name="insert" value="Sign In"> 
      <a class="reset_pass" href="#forgetpass">Lost your password?</a> 
     </div> 
     <div class="clearfix"></div> 
     <div class="separator"> 
      <p class="change_link">New to site? 
      <a href="#signup" class="to_register"> Create Account </a> 
      </p> 

      <div class="clearfix"></div> 
      <br /> 

      <div> 
      <h1><i class="fa fa-paw"></i> DiGItal Society</h1> 
      <p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p> 
      </div> 
     </div> 
     </form>    
    </section> 
    </div> 

    <div id="register" class="animate form registration_form"> 
    <section class="login_content"> 
     <form action="insertUser.php" method="post"> 
     <h1>Create Account</h1> 
     <div> 
      <input type="text" class="form-control" placeholder="Username" required="" name="username" /> 
     </div> 
     <div> 
      <input type="email" class="form-control" placeholder="Email" required="" name="email" /> 
     </div> 
     <div> 
      <input type="password" class="form-control" placeholder="Password" required="" name="password" /> 
     </div> 
     <div> 
      <input type="hidden" name="roleid" value=""> 
      <input type="submit" class="btn btn-default" name="insert" value="Log In"> 
     </div> 

     <div class="clearfix"></div> 

     <div class="separator"> 
      <p class="change_link">Already a member ? 
      <a href="#signin" class="to_register"> Log in </a> 
      </p> 

      <div class="clearfix"></div> 
      <br /> 

      <div> 
      <h1><i class="fa fa-paw"></i> DiGItal Society</h1> 
      <p>©2016 All Rights Reserved. DiGItal Society is a Web Portal for E-Society. Privacy and Terms</p> 
      </div> 
     </div> 
     </form> 
    </section> 
    </div> 
</div> 

我還添加了我的操作頁面(不工作)。 login.php爲第一種形式(不工作)

<?php 
include 'db.php'; 
if (isset($_REQUEST['insert'])) 
    { 
    echo $username = $_REQUEST['username']; 

    echo $password = $_REQUEST['password']; 
     $sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'"); 
     $data = mysqli_fetch_array($conn,$sql); 
     $_SESSION['role']=$data['roleId']; 
     $_SESSION['username']=$data['acc_name']; 
     $data = mysqli_num_rows($data); 
     if ($data>0) 
      { 
       header('Location: home.php'); 
      } 
     else 
      { 
       header('Location: index.php'); 
       echo 'incorrect login'; 
      } 
    } 
?> 
爲第二形式

insertUser.php。(工作)

<?php 
include 'db.php'; 
if (isset($_REQUEST['insert'])) 
    { 
     $acc_name = $_REQUEST['username']; 
     $acc_email = $_REQUEST['email']; 
     $acc_pass = $_REQUEST['password']; 
     $role_id = $_REQUEST['roleid']; 
     $sql = mysqli_query($conn,"INSERT INTO `accountants`(`acc_name`, `acc_email`, `acc_pass`, `roleId`) VALUES ('".$acc_name."','".$acc_email."','".$acc_pass."','2')"); 
     if ($sql>0) 
      { 
       header('Location: home.php'); 
       echo 'data added successfully'; 
      } 
     $row = mysqli_query('SELECT * FROM `accountants`');  
     $data = mysqli_fetch_array($row); 
     $data = mysqli_num_rows($conn,$data); 
     $_SESSION['role'] = $data['roleId']; 
    } 
?> 
+0

在輸入提交中爲第一個表單嘗試使用不同的'name'。並使用它在login.php –

+0

@KinshukLahiri好吧,我嘗試它但不工作。還在同一頁 –

+0

這個'print_r($ _ REQUEST)的輸出是什麼?的print_r($ _ POST [ 「插入」])';死;'爲login.php文件。 –

回答

0

這裏是解決方案:試試這個:

裏面的,如果條件:

if (isset($_REQUEST['insert'])) 

將上面的內容更改爲:

if (isset($_REQUEST['login'])) 

PS:更改這些以及:

echo $username = $_REQUEST['username']; 

echo $password = $_REQUEST['password']; 

到:

echo $username = $_REQUEST['user']; 

echo $password = $_REQUEST['pass']; 

希望它能幫助。

+0

仍然不工作兄弟。我早些時候嘗試過。不知道這個問題 –

+0

@amitsutar更新。 –

+0

我已經改變此部分 –

0

嘗試從表單登錄在<input type="submit" class="btn btn-default" name="insert" value="Sign In">重命名name="insert"。 因爲名字相同。

例如<input type="submit" class="btn btn-default" name="login" value="Sign In">

,不要忘了重新命名的log​​in.php

<?php 
include 'db.php'; 
if (isset($_REQUEST['login'])) 
    { 
    echo $username = $_REQUEST['username']; 

    echo $password = $_REQUEST['password']; 
     $sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'"); 
     $data = mysqli_fetch_array($conn,$sql); 
     $_SESSION['role']=$data['roleId']; 
     $_SESSION['username']=$data['acc_name']; 
     $data = mysqli_num_rows($data); 
     if ($data>0) 
      { 
       header('Location: home.php'); 
      } 
     else 
      { 
       header('Location: index.php'); 
       echo 'incorrect login'; 
      } 
    } 
?> 
+0

我試過這個兄弟不工作 –

+0

輸入insertUser.php並登錄。PHP相同....輸入名稱密碼和用戶名。 –

0

下面是解決方案修復您的login.php頁面。

<?php 

if (isset($_POST['insert'])) 
    { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 

     $sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '$username' AND `acc_pass` = '$password'"); 
     $data = mysqli_fetch_array($conn,$sql); 
     $_SESSION['role']=$data['roleId']; 
     $_SESSION['username']=$data['acc_name']; 
     $data = mysqli_num_rows($data); 
     if ($data>0) 
      { 
       header('Location: home.php'); 
      } 
     else 
      { 
       header('Location: index.php'); 
       echo 'incorrect login'; 
      } 
    } 
?> 
+0

nop bro不起作用。 –

+0

你收到了什麼錯誤?仍然頁面正在重新加載? –

+0

沒有得到任何錯誤只是頁面重新加載onclick就是這樣,沒有任何反應。 –