首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何让我的html/php代码从一个提交按钮直接转到两个不同的页面?

我如何让我的html/php代码从一个提交按钮直接转到两个不同的页面?
EN

Stack Overflow用户
提问于 2018-10-20 11:11:02
回答 2查看 51关注 0票数 0

根据输入的用户名和密码组合,我尝试使用PHP指向不同的表单: ssmith转到main_menu.php,tsmith转到menu.php。我试过所有我能想到的东西,但我在谷歌上找不到任何东西。有人知道怎么做吗?当我点击“登录”按钮时,不管我放的是ssmith还是tsmith,它总是转到main_menu.php。下面是我当前的代码:

代码语言:javascript
运行
复制
<?php

//Connect to database
include "db_connect.php";

//Get variable from login form
$username=$_POST['username'];
$password=$_POST['password'];

//Check if any text has been entered in username and password
if ((empty($username)) || empty($password)){
echo "Please enter a username or password. Go back to the <a 
href='Index.php'>login page</a>";
}

else {

//Check to see if username and password is found in table
$sql="SELECT * FROM accounts WHERE Username='ssmith'";

//Place the result of the sql query into the variable $result
$result=$mysqli->query($sql);

if($result=ssmith){

//Display main menu page
header("location:main_menu.php");

}

else {
//Check to see if username and password is found in table
$sql="SELECT * FROM accounts WHERE Username='tsmith'";

//Place the result of the sql query into the variable $result
$result=$mysqli->query($sql);

if($result=tsmith){

//Display menu page
header("location:menu.php");


}

else {

//Display error message
echo "Please username and password could not be found. Go back to the <a 
href='Index.php'>login page</a>";

}


}

}

?> 
EN

回答 2

Stack Overflow用户

发布于 2018-10-20 11:34:03

首先检查用户名中的U字母。是不是和你在数据库里用的名字一样。否则,请使用以下代码

代码语言:javascript
运行
复制
    //first search anything from accounts
$sql="SELECT * FROM accounts";

//then navigate header according to the result
$result_query=$mysqli->query($sql);

if (mysqli_num_rows($result_query) > 0){
    while($result = mysqli_fetch_assoc($result_query)){
        if($result['username'] == 'ssmith'){
           header("location:main_menu.php");
        }elseif($result['username'] == 'tsmith'){
           header("location:menu.php");
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-10-20 12:18:57

也许在考虑到安全性的情况下,一些正则表达式可能会有所帮助。

代码语言:javascript
运行
复制
if(preg_match("/ssmith/", $result)){
$url = "main_menu.php";
}
else if(preg_match("/tsmith/", $result)){
$url = "menu.php";
}
else{
echo "Please username and password could not be found. Go back to the <a 
href='Index.php'>login page</a>";
}

header("location: $url");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52902089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档