php - 使用 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'。
解决方案
推荐阅读
- android - 如何使用此代码实现下一个和上一个按钮?
- audio - 使用 FFmpeg 将多个音频文件交叉淡化为一个?
- android - 无法运行 adb 进程
- python - 如何将列表中的某些分数分配给多个列表中的值并获取python中每个值的总和?
- c++ - 我在主函数的左括号上不断收到错误消息 obj\Debug\main.o||In function `ZN11linked_listC1Ev':|
- idris - 关于 `mod` 的 Idris 证明
- java - Java继承如何区分程序中的用户类型?
- java - 我可以在 BIRT 4.7 报表设计器中使用 SSL 证书连接到 MySQL 吗?
- imageview - 如何在 Android 8.0 Oreo 上调整图像视图的大小/缩放类型?
- python-3.x - Python: Use a for loop to get Nth number out of a list