表单操作无法在php中运行?

时间:2017-04-06 06:16:22

标签: php forms action

我在一个页面中基本上有2个表单。 第一个用于登录,第二个用于插入数据。 第二种形式的行动正常。我可以用它插入数据。 但我用于登录用户的形式相同,但它不起作用。 点击提交按钮,页面刷新时没有任何反应。

请查看我的代码并帮助我解决我的第一个表格的行动问题。

<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'];
    }
?>

3 个答案:

答案 0 :(得分:0)

尝试从表单登录重命名name="insert"中的<input type="submit" class="btn btn-default" name="insert" value="Sign In">。 因为名字是一样的。

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

并且不要忘记重命名login.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';
            }
    }
?>

答案 1 :(得分:0)

以下是解决方案:试试这个:

在if条件中:

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'];

希望它有所帮助。

答案 2 :(得分: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';
            }
    }
?>