实际上,我想根据以下条件从我的表中获取数据, table包含5列,name,user_id,role,reporting_manager,password。我为了给角色写了一个Html页面。
<html>
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><!--Dquip--></title>
<link href="http://fonts.googleapis.com/css?family=Abel|Arvo" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.dropotron-1.0.js"></script>
<script type="text/javascript" src="jquery.slidertron-1.0.js"></script>
<style>
label
{
display:inline-block;
width:150px;
margin-right:10px;
text-align:;
}
table,tr,th,td
{
border: 2px solid dodgerblue;
border-collapse: separate;
}
table
{
width:75%;
margin-top: 8%;
}
th,td
{
height: 50px;
}
td
{
text-align:center;
vertical-align: middle;
}
.button
{
width:75px;
height: 25px;
background-color:dodgerblue;
color:white;
border:1px solid transparent;
}
</style>
<center>
</head>
<body>
<h4 align="right"><a href="loginpage.php">Logout</a></h4>
<body>
<div id="wrapper">
<div id="header-wrapper">
<div id="header">
<div id="logo">
<h1><a href="#">Dquip..</a></h1>
</div>
</div>
<div id="menu-wrapper">
<ul id="menu">
<li class="current_page_item"><a href="calenderindex.html"><span>Homepage</span></a></li>
<li><span>Blog</span>
<ul>
<li class="first"> <a href="index">About US</a> </li>
<li> <a href="search">Function Area</a> </li>
<li class="last"> <a href="about">Contact US</a> </li>
</ul>
</li>
<li><a href="#"><span>Photos</span></a></li>
<li><a href="#"><span>About</span></a></li>
<li><span>Links</span>
<ul>
<li class="first"> <a href="index.php">Add Details</a> </li>
<li> <a href="map.php">Map view</a> </li>
<li class="last"> <a href="fetchinghome.php">view Details</a> </li>
</ul>
</li>
<li><a href="#"><span>Contact</span></a></li>
</ul>
<script type="text/javascript">
$('#menu').dropotron();
</script>
</div></br>
<h1><font color="white">Enter the dates to retrieve the data</font></h1></br></br>
<form method="POST" action="fetchinghome.php">
<label>Your user ID :</label><input type="text" name="role" placeholder="Enter the starting date">
<input type="submit" id="submit" name="submit" value="Go" class="button">
</form>
</body>
</html>
<?php
//include "loginpage.php";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_POST['submit']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "calender";
$role=$_REQUEST['role'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
$sql="SELECT * FROM registration where reporting_manager='$role'";
$retval = mysqli_query( $conn,$sql );
if(! $retval )
{
die('Could not get data: ' . mysqli_error($conn));
}
echo "<table id='example' class='display' cellspacing='0' width='100%'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>Address</th>
<th>Role</th>
<th>Action</th>
</tr>";
while($row = mysqli_fetch_array($retval))
{
echo "<tr><td>";
echo $row[0]."</td><td>";
echo $row[1]."</td><td>";
echo $row[2]."</td><td>";
echo $row[3]."</td><td>";
echo $row[4]."</td>";
echo "</tr>";
}
echo "</table>";
}
echo "Fetched data successfully\n";
mysqli_close($conn);
}
?>
如果我以管理员身份角色,则需要显示数据库中的所有数据。 如果我将角色作为manager1,则显示其reporting_manager为manger1的用户的数据加上manager1个人的详细信息。 如果我作为user1角色,只获取该用户详细信息。 是否可以使用if else在mysql中编写此查询。如果有人对此有任何想法,请告诉我。
答案 0 :(得分:1)
你可以在条件中设置$ sql变量,例如
if ($role == 'admin')
{
$sql="SELECT * FROM registration";
}
else
{
$sql="SELECT * FROM registration where reporting_manager='$role'";
}