0
我试图设置不同的访问使用用户和管理员,但我无法弄清楚如何让他们看起来不同,我试图设置tinyint数据库,其中1是管理员和0是用户,但我不认为我做对了。 (你在其他地方找到它的代码),所以如果有更好的方法,帮助将非常感激。先谢谢你。 php创建单独的用户/管理员登录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Homeowners Association</title>
</head>
<?php
if ($_SESSION['validUser'] == "yes")
//is this already a valid user?
{
//turn off PHP and turn on HTML
?>
<h1>Display Events Admin Options</h1>
<p><a href="insertEvent.html">Input New Events</a></p>
<p><a href="selectEventsProtected.php">List of Events</a></p>
<p><a href="logout.php">Logout</a></p>
<?php //turn off HTML and PHP
}
else
{
if (isset($_POST['submitLogin']))
//Was this page called from a submitted form?
{
$inUsername = $_POST['loginUsername'];
//pull the username from the form
$inPassword = $_POST['loginPassword'];
//pull the password from the form
include ('dbConnect.php');
//Connect to the database
$sql = "SELECT * FROM homeowners WHERE username = '" . $inUsername . "' AND password = '" . $inPassword . "'";
//this SQL command will only work if BOTH the username and password on the table
$result = mysqli_query($link,$sql) or die("SQL Error " . mysqli_error($link) . "<p>SQL String: $sql</p>");
if (mysqli_num_rows($result) == 1)
//If this is a valid user there should be ONE row only
{
$_SESSION['validUser'] = "yes";
//this is a valid user so set your SESSION variable
//turn off PHP and begin HTML
?>
<h1>Display Events Admin Options</h1>
<p><a href="insertEvent.html">Input New Event</a></p>
<p><a href="selectEventsProtected.php">List of Events</a></p>
<p><a href="logout.php">Logout</a></p>
<?php //turn off HTML and turn on PHP
}
else
//This is an invalid user not in the database
{
echo "<h3>Invalid username or password. Please try again.</h3>"; //sets error message
//display login form again with the error message.
//turn off PHP and begin HTML
?>
<form method="post" name="login" action="login.php" >
<p>Username: <input name="loginUsername" type="text" /></p>
<p>Password: <input name="loginPassword" type="password" /></p>
<p><input name="submitLogin" value="Login" type="submit" /> <input name="" type="reset" /> </p>
</form>
<?php //turn off HTML and turn on PHP
}//end of checking for a valid user
}//end of checking for a submitted page
else //This page was not submitted so the user needs to se the sign on form to continue
{
//display the login form in the area below
//turn off PHP and begin HTML
?>
<h1>Login to access website</h1>
<form method="post" name="loginForm" action="login.php">
<p>Username: <input name="loginUsername" type="text" /></p>
<p>Password: <input name="loginPassword" type="password" /></p>
<p><input name="submitLogin" value="Login" type="submit" /> <input name="" type="reset" /> </p>
</form>
<?php //turn off HTML and turn on PHP
}//ends if statement to check for form submit
}//end if checking for a valid user
//turn off PHP and begin HTML
?>
</body>
</html>
我试着使用:
$query = mysqli_query("SELECT type FROM homeowners WHERE username = '$user'");
$gettype = mysqli_fetch_assoc($query);
if($gettype["type"] == 0){
echo("user");
}
elseif($gettype["type"] == 1){
echo("admin");
}
,但我不知道有足够的了解TINYINT(我已经设置为输入)
是这个答案使用tinyint或枚举(我不能使用导致数据库说每次我尝试时都有错误)? – Felicyia 2014-12-06 06:04:14
这个答案是使用tinyint,你可以发布你的数据库表吗? – 2014-12-06 06:49:13
我不知道如何将它张贴到我的服务器作为jpg http://felicyiamcclelland.com/introphp/homeowners/images/server.jpg – Felicyia 2014-12-06 06:58:46