dousi2013 2015-05-24 21:44
浏览 20
已采纳

登录信息页面不重定向

I am having trouble redirecting my code to another page after a correct username/password has been submitted. After I enter the username/password and hit submit, the page does not redirect.

The page just reloads and clears the field inputs (even though it's a sticky form), and stays at the same page, except the URL adds ?submit=Submit to the end of the URL..

I am not getting any mysql errors - the database is connecting fine at the query that matches the row with the username/password combo seems to be going through.

It should be looping through the database, checking if the username/password combo exists, and redirecting to the specified page. I'm not sure what I'm not seeing / missing here that keeps the page from redirecting. Any input would be greatly appreciated.

PHP:

<?php
        define('DB_LOCATION', 'x');
        define('DB_USERNAME', 'x');
        define('DB_PASS', 'x');
        define('DB_NAME', 'x');

        $dbc = mysqli_connect(DB_LOCATION, DB_USERNAME, DB_PASS, DB_NAME)
            or die('Error connecting to database');

    $error_message= "";

    if (isset($_POST['submit'])) {

        $user_name = $_POST['user'];
        $user_password= $_POST['pass'];


        // ADD QUERY TO CHECK IF USER/PASS COMBO IS CORRECT
        if(!empty($user_name) && !empty($user_password)) {

        $query = "SELECT * FROM employees WHERE username='$user_name' and password='$user_password'";

        $result = mysqli_query($dbc, $query)
            or die ('Error querying username/password request');

            if(mysqli_num_rows($result) == 1) {

                $user_name = $row['user'];
                $user_password = $row['pass'];

                header("Location: www.mysite.com ");

            } // end if rows

            else {
                $error_message = "You were not able to log in";
            } // end else


        } // end query


    } // end isset

?>

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Login</title>
  <link type="text/css" rel="stylesheet" href="/LESSON5/5_Signup_CSS.css">

</head>
<body>
<h1>Welcome to my website!</h1>
<h2>Please login below.</h2>
<h3>Don't have an account? <a href="/LESSON5/2%20-%20CREATE%20AN%20ACCOUNT.php">Create one here.</a></h3>

<div class="formFormat" >  
<div  id="table1">
<form name =loginForm method="post" action="<?php echo $_SERVER[' PHP_SELF' ];?>">
  <table id="cssTable">
    <tr>
        <td>Username:</td><td><input type="text" id="user" name="user" value="<?php echo $user_name ?>" /></td>
    </tr>
    <tr>
        <td>Password:</td><td><input type="text" id="pass" name="pass" value="<?php echo $user_password ?>"/></td>
    </tr>
      </table>
  </form>
  </div>

  <div id="table2">
  <form>
  <table> 
  <tr>
     <td><input type="submit" name="submit"/></td>
   </tr>
   <tr>
      <td id="createAccount"><a href="/LESSON5/2%20-%20CREATE%20AN%20ACCOUNT.php">Create an account</a></td>
   </tr>
   <tr>
    <td><?php echo $error_message ?></td>
   </tr>

  </table>
</form>
  </form>
  </div>
</div> 

<?php
    mysqli_close($dbc);
?>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • douyu4535 2015-05-24 21:51
    关注

    Remove <form>, just before <div id="table2">, and </form> as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)