首页 > 解决方案 > 使用 PHP 和 MYSQL 重定向到客户页面

问题描述

我对 PHP 相当陌生。我有一个带有两个框(用户名和密码以及一个按钮(提交按钮)的登录页面。

我有一个包含注册用户列表的 mysql 数据库。当用户登录时,我希望他们转到分配给他们的页面。一个例子是您登录您的帐户并进入您的页面的 Facebook。

这里例如:

if($user == "Sophie")
{
<script type="text/javascript">window.location.href="sophie.html"</script>
}

这会将用户重定向到正确的页面。

但我不希望每个用户都有多个 switch 或 else if 语句。我怎样才能有一个 if 语句将用户重定向到他们的正确页面,具体取决于他们在文本框中输入的内容。

感谢您提供的任何帮助。

我在网上查过,但找不到任何东西。我已经玩了两天了,还没有找到解决方案。

<!doctype html>
<?php

$servername = “jkjones.com";
$username = “men000”;
$password = “password123”;
$db = “customer”;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
else
{
    echo "Connection Successful to: " . $username. "<br>" ;

}

if(isset($_POST["Login"]))
{
    $user = $_POST["user"];
    $pass = $_POST["pass"];
    $query = "SELECT * FROM testTable WHERE name='".$user."' and password='".$pass."'";
    $result = mysqli_query($conn,$query);
    $myrow=mysqli_fetch_array($result);
    if($result)
    {        
        while($row=mysqli_fetch_array($result))
        {
            echo('<script type="text/javascript">alert("You are logged in successfully and log in as: '.$row['$user'].'")</script>');
        }
        if(mysqli_num_rows($result)>0)
        {
            if($myrow['$user'] == $user)
            {
                echo '<script>location.href = "EDDIE.php?"</script>';
                ?>
                <?php       
            }
            else
            {
?>
            <script type="text/javascript">window.location.href="index.html"</script>
<?php   
            }
        }
        else
        {
            ?>
            <script type="text/javascript">window.location.href="errorpage.html"</script>
<?php
        }
    }
}

?>

<html>
<head>
<meta charset="UTF-8">
<title>Login-Index</title>
</head>

<body>

    <form method="post">
    <table>
        <tr>
        <td>Username:<input type="text" name="user" placeholder="Enter your user name."></td>
        </tr>
        <tr>
        <td>Password:<input type="text " name="pass" placeholder="Enter your password."></td>
        </tr>
        <tr><td></td></tr>
        <tr>
        <td><input type="submit" name="Login" value="Log me In"></td>
        </tr>
        </table>
    </form>
</body>
</html> 

当前的结果是它回到了 else 语句中的 'index.html'。

标签: phpmysql

解决方案


推荐阅读