*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能

PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能

PHP实现三表联查,实现输入用户id查询出用户姓名、用户角色,输入角色id查询所有用户的ID、姓名和角色,最后实现向数据库中添加用户的功能。

前台页面

*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能_第1张图片

表结构

user表

*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能_第2张图片

role表

*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能_第3张图片

user_role表

*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能_第4张图片

代码

data.html

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>用户查询</title>
 </head>
 <body>
 
<form method="post" action="selectUser.php" name="search">
查询用户ID<input name="searchUserId" type="text" value="" size="15"/>
<input type="submit" value="Search" />
</form>
<br>
<br>

<form method="post" action="selectRole.php">
查询角色ID<input name="searchRoleId" type="text" value="" size="15"/>
<input type="submit" value="Search">
</form>
<br/>
<hr/>
 
添加用户
 
 <div>
 <form action="user_add.php" method="post">
用户ID&nbsp&nbsp&nbsp<input type="text" name="user_id" /><br/>
用户姓名:<input type="text" name="user_name" /><br/>
<br/>
<input type="submit" value="添加" />
</form>
</div>
 </body>
</html>

selectUser.php

<?php
header("Content-Type:text/html;charset=utf-8");
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "test";

		//创建连接
		$conn = new mysqli($servername,$username,$password,$dbname);
		//检测连接
		if ($conn->connect_error){
			die("连接失败")  . $conn->connect_error;
		}else{
			//var_dump($conn);
			//die("数据库连接成功!!!");
			// var_dump(111);
		}
 $searchUserId = $_POST['searchUserId'];  //传递search框过来的值
 $searchUserId = trim($searchUserId);
 
if (!$searchUserId && !$searchRoleId)
{
echo '搜索框不能为空.';
exit;
}

$sql = "SELECT u.user_name,r.role_name FROM user as u inner join user_role as ur on u.user_id=ur.user_id inner join role as r on r.role_id=ur.role_id WHERE u.user_id = $searchUserId";
$result = $conn->query($sql);
// echo $sql;
// echo "
";
if($result->num_rows > 0){ //输出数据 while ($row = $result->fetch_assoc()) { echo "用户姓名:" . $row["user_name"] . " " . " 用户角色:" . $row["role_name"]; echo "
"
; } }else{ echo "没有该用户!"; } $conn->close();

selectRole.php

<?php
header("Content-Type:text/html;charset=utf-8");

 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "test";

		//创建连接
		$conn = new mysqli($servername,$username,$password,$dbname);
		//检测连接
		if ($conn->connect_error){
			die("连接失败")  . $conn->connect_error;
		}else{
			//var_dump($conn);
			//die("数据库连接成功!!!");
			// var_dump(111);
		}

$searchRoleId = $_POST['searchRoleId'];  //传递search框过来的值
$searchRoleId = trim($searchRoleId);


$sql2 = "SELECT ur.user_id,u.user_name,r.role_name FROM role as r inner join user_role as ur on r.role_id=ur.role_id inner join user as u on u.user_id=ur.user_id WHERE r.role_id = $searchRoleId";
$res = $conn->query($sql2);
if ($res->num_rows > 0) {
	while($row = $res->fetch_assoc()){
		echo "用户ID:" . $row['user_id'] . " " . "用户姓名:" . $row['user_name'] . " " . " 用户角色: " . $row['role_name'];
        echo "
"
; } }else{ echo "没有该角色!"; }

user_add.php

<?php
header("Content-Type:text/html;charset=utf-8");

$servername = "localhost";
$username = "root";
$password = "";
$db_name = "test";

//创建连接
$conn = new mysqli($servername,$username,$password,$db_name);
//检测连接
if ($conn->connect_error) {
	die("连接失败......");
}else{ 
	echo "连接成功!";
}

$user_id = $_POST['user_id'];
$user_name = $_POST['user_name'];

$sql = "INSERT into user(user_id,user_name)Values('$user_id','$user_name')";
if ($conn->query($sql) === TRUE) {
	echo "新纪录插入成功!";
}else{
	echo "Error" . $sql . "
"
. $conn->error; } $conn->close();

你可能感兴趣的:(*PHP实现三表联查,实现根据id查询数据 ,并向数据库中添加数据的功能)