不看讲解视频,自己写出来
main.css
.text0{
font-size:25px;
font-weight:bolder;
}
.text1{
font-size: 15px;
}
.text2{
font-size: 15px;
}
.text3{
font-size: 18px;
font-weight:bold;
}
main.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息主页面</title>
<style>
</style>
</head>
<link rel="stylesheet" href="main.css">
<body>
<center>
<!-- 1.title -->
<p class="text0">学生信息管理系统</p>
<!-- #1.跳转别的自己编写的页面way -->
<a href="main.php" class="text1">浏览信息</a>
<a href="insert.php" class="text1">添加信息</a>
<!-- #2.水平线 -->
<hr>
<br>
<p class="text3" >学生信息浏览</p>
<!-- #3.input背景文字 -->
<input placeholder="请输入查询信息" ><button>查询</button>
<br>
<?php
// print_r($_POST);
// 输出
if($_SERVER['REQUEST_METHOD'] === 'POST'){
echo '
';
echo "学号 | '; echo '姓名 | '; echo '性别 | '; echo '电话 | '; echo '年龄 | '; echo '学院 | '; echo '
---|---|---|---|---|---|
{$_POST['stu_no']} | "; echo "{$_POST['stu_name']} | "; echo "{$_POST['gender']} | "; echo "{$_POST['telephone']} | "; echo "{$_POST['age']} | "; echo "{$_POST['college']} | "; echo '
inset.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加信息页面</title>
</head>
<link rel="stylesheet" href="main.css">
<body>
<body>
<center>
<!-- 1.title -->
<p class="text0">学生信息管理系统</p>
<!-- #1.跳转别的自己编写的页面way -->
<a href="main.php" class="text1">浏览信息</a>
<a href="insert.php" class="text1">添加信息</a>
<!-- #2.水平线 -->
<hr>
<br>
<form method="post" action="main.php">
<table>
<tr>
<td>学号:</td>
<td><input type="text" name="stu_no"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="stu_name" ></td>
</tr>
<tr>
<td>姓别:</td>
<td>
男:<input type="radio" name="gender" value="男">
女:<input type="radio" name="gender" value="女">
</td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="telephone"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>学院:</td>
<td><input type="text" name="college"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="添加">
<input type="reset" name="reset" value="重置"><br>
<span class="text3">GET</span>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<!-- 1.接受输入框的数据,并且传给MySQL
1.1 在此页面输入框输入并且在此页面中打印 --ok
1.1.1 在main页面中打印 --ok
1.2 使用MySQL接收 -->
<!-- else -->
<!-- 1.GET怎么改POST 试1-->
<!-- 2.main表格边框改为蓝色 -->
没学过,怎么把数据从MySQL中打印出来
目的:不要死板的按着上面一步步做,看自己需要的,按自己的思路做,最后再回顾一遍,总结我的方法和他的差距
遍历二维数组方法
文章:【web前端开发】后台PHP
https://blog.csdn.net/m0_65431212/article/details/126911190
1.使用MySQL进行存储
2.使用数组遍历进行打印
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息主页面</title>
<style>
</style>
</head>
<link rel="stylesheet" href="main.css">
<body>
<center>
<!-- 1.title -->
<p class="text0">学生信息管理系统</p>
<!-- #1.跳转别的自己编写的页面way -->
<a href="main.php" class="text1">浏览信息</a>
<a href="insert.php" class="text1">添加信息</a>
<!-- #2.水平线 -->
<hr>
<br>
<p class="text3" >学生信息浏览</p>
<!-- #3.input背景文字 -->
<input placeholder="请输入查询信息" ><button>查询</button>
<br>
<?php
//配置变量
$dbtype = "mysql";//设置数据库类型
$host = "localhost";//设置主机名称
$dbname = "mydemo";//设置默认数据库名称
$username = "root";//设置数据库用户名
$password = "";//设置数据库用户密码
// //获取数据库
// $pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$username,$password);
// var_dump($pdo);
try{
//获取数据库连接
$pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$username,$password);
var_dump($pdo);
echo "连接成功";
}
catch(PDOException $e){
print "Error!: ". $e->getMessage() ."
";
die();
}
?>
</center>
</body>
</html>
<input placeholder="请输入查询信息" >
9.表格属性详解
看这个文章
https://blog.csdn.net/TDCQZD/article/details/82226260?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166376330616782428699984%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166376330616782428699984&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~hot_rank-3-82226260-null-null.142v49control,201v3add_ask&utm_term=%E8%A1%A8%E6%A0%BC%E6%A0%87%E7%AD%BE%E5%B1%9E%E6%80%A7&spm=1018.2226.3001.4187
<th colspan="2">操作th>
11. 使用表格功能布局多个输入框情况
把boder设置为0
12. 第一次进入页面默认为get请求,如果自己设置了post,第一次进入会报错,解决方式:
if($_SERVER["REQUEST_METHOD"] === 'POST')
if($result > 0){
header('Location:index.php');
}
echo "删除 ";
新建delete.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
require 'common/conn.php';
$stu_get = $_GET['stu_no'];
print_R($stu_get);
$sql = "DELETE from stu where stu_no='{$stu_get}'";
$result = $pdo->exec($sql);
header('Location/index.php');
?>
</body>
</html>
1.输入框name使用数据库功能
$sql = "SELECT * from stu";
// print_r($sql);
if(isset($_GET['select'])){
print_r(($_GET['select']));
}
2.PHP的拼接技术,实现查询功能
分开作用:新赋值查询的条件$select
注意:where前面空一格!!!
代码改进:
$sql = "SELECT * from stu";
// print_r($sql);
if(isset($_GET['select'])){
print_r(($_GET['select']));
$select =$_GET['select'];
$sql .= " where stu_no='{$select}'";
}
语法:
以“土木工程”为例:
“where 数据库字段 like ‘PHP变量%’”; //输入土木可以检索
“where 数据库字段 like ‘%PHP变量’”; //输入工程可以检索
“where 数据库字段 like ‘%PHP变量%’”; //输入什么都可以
一般:把两个%都写上
代码改进:
if(isset($_GET['select'])){
print_r(($_GET['select']));
$select =$_GET['select'];
$sql .= " where stu_no like '%$select%'";
}
一维数组方法fetch
删去add.php抄过来的内容,改写:
include 'common\menu.php';
require 'common\conn.php';
$stu_get = $_GET['stu_no'];
$sql = "SELECT * from stu where stu_no = '{$stu_get}'";
$result = $pdo -> query($sql);
$stu_arr = $result -> fetch(PDO::FETCH_ASSOC);
print_r($stu_arr);
?>
改进代码:
<form action="" method="post">
<table>
<tr>
<td>学号:</td>
<td><input type="text" name="stu_no" value="$stu_arr['stu_no'] ?>"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="stu_name" value="$stu_arr['stu_name'] ?>"></td>
</tr>
<tr>
<td>姓别:</td>
<td>
男:<input type="radio" name="gender" value="男" <?php
if($stu_arr['gender']=='男'){
echo 'checked';
}
?>>
女:<input type="radio" name="gender" value="女"<?php
if($stu_arr['gender']=='女'){
echo 'checked';
}
?>>
</td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="telephone" value="$stu_arr['telephone'] ?>"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" value="$stu_arr['age'] ?>"></td>
</tr>
<tr>
<td>学院:</td>
<td><input type="text" name="college" value="$stu_arr['college'] ?>"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="修改">
<input type="reset" name="reset" value="重置">
</td>
</tr>
</table>
</form>
print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
if($_SERVER["REQUEST_METHOD"] === 'POST'){
print_r($_POST);
$stu_no = $_POST['stu_no'];
$stu_name = $_POST['stu_name'];
$gender = $_POST['gender'];
$telephone = $_POST['telephone'];
$age = $_POST['age'];
$college = $_POST['college'];
}
$sql = "UPDATE stu set stu_no='{$stu_no}',stu_name='{$stu_name}',
gender='{$gender}',telephone='{$telephone}',age='{$age}',college='{$college}'
where stu_no='{$stu_get}'";
$result = $pdo -> exec($sql);
print_r($result);
if($result > 0){
header('Location:index.php');
}else{
echo '修改失败';
}
?>
<!-- 连接数据库 -->
<?php
//配置变量
$dbtype = "mysql";//设置数据库类型
$host = "localhost";//设置主机名称
$dbname = "mydemo";//设置默认数据库名称
$username = "root";//设置数据库用户名
$password = "";//设置数据库用户密码
//创建数据源
$dsn = "{$dbtype}:host={$host};dbname={$dbname}";
// //获取数据库
// $pdo = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$username,$password);
// var_dump($pdo);
try{
//获取数据库连接
$pdo=new PDO($dsn,$username,$password);
$pdo->query("set names utf8");
// echo "连接成功";
}
catch(PDOException $e){
die('连接失败' . $e->getMessage());
}
?>
<!-- 公共导航栏 -->
<!-- 1.title -->
<!-- <link rel="stylesheet" href="1.css"> -->
<h2>学生信息管理系统</h2>
<a href='index.php'>浏览信息</a>
<a href='add.php'>添加信息</a>
<hr>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息主页面</title>
</head>
<link rel="stylesheet" href="1.css">
<body>
<center>
<?php
// 1.公共上端
include 'common\menu.php';
require 'common\conn.php';
?>
<!-- 2.本页简单元素 -->
<br>
<h3>学生信息浏览</h3>
<!-- <input placeholder="请输入查询信息" ><button>查询</button> -->
<!-- 查询信息框 -->
<form action="" method="get">
<input type="text" name="select"
placeholder="请输入查询信息">
<input type="submit" value="查询">
</form>
<!-- 3.打印表格 -->
<table border="1" width="800" cewllspacing="0"
cellpadding="5" bordercolor="green" style="font-family:'楷体';">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
<th>年龄</th>
<th>学院</th>
<th colspan="2">操作</th>
</tr>
<!-- 4.返回数据库中学生数据的二维数组数据-->
<?php
$sql = "SELECT * from stu";
// print_r($sql);
if(isset($_GET['select'])){
print_r(($_GET['select']));
$select =$_GET['select'];
$sql .= " where stu_no like '%$select%'";
}
$result = $pdo->query($sql);
// print_r($result);
$stu_arr = $result->fetchALL(PDO::FETCH_ASSOC);
// print_r($stu_arr);
// 5.遍历二维数组并且打印
foreach($stu_arr as $stu){
echo "";
echo "{$stu['stu_no']} ";
echo "{$stu['stu_name']} ";
echo "{$stu['gender']} ";
echo "{$stu['telephone']} ";
echo "{$stu['age']} ";
echo "{$stu['college']} ";
echo "]}'>修改 ";
echo "]}'>删除 ";
echo " ";
}
?>
</table>
</center>
</body>
</html>
<!-- 浏览页面 查找页面 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加信息页面</title>
</head>
<body>
<center>
<?php
include 'common\menu.php';
require 'common\conn.php';
?>
<br>
<h3>添加学生信息</h3>
<!-- 1.输入框设置 -->
<form action="" method="post">
<table>
<tr>
<td>学号:</td>
<td><input type="text" name="stu_no"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="stu_name"></td>
</tr>
<tr>
<td>姓别:</td>
<td>
男:<input type="radio" name="gender" value="男" check>
女:<input type="radio" name="gender" value="女">
</td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="telephone"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>学院:</td>
<td><input type="text" name="college"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="添加">
<input type="reset" name="reset" value="重置">
</td>
</tr>
</table>
</form>
<?php
// 2.数据库添加的组合代码
print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
if($_SERVER["REQUEST_METHOD"] === 'POST'){
print_r($_POST);
$stu_no = $_POST['stu_no'];
$stu_name = $_POST['stu_name'];
$gender = $_POST['gender'];
$telephone = $_POST['telephone'];
$age = $_POST['age'];
$college = $_POST['college'];
$sql = "INSERT into stu(stu_no,stu_name,gender,telephone,
age,college) values('{$stu_no}','{$stu_name}','{$gender}','{$telephone}'
,'{$age}','{$college}')";
$result = $pdo -> exec($sql);
print_r($result);
if($result > 0){
header('Location:index.php');
}else{
echo '添加失败';
}
}
?>
</center>
</body>
</html>
<!-- 添加页面 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修改信息页面</title>
</head>
<body>
<center>
<?php
include 'common\menu.php';
require 'common\conn.php';
$stu_get = $_GET['stu_no'];
$sql = "SELECT * from stu where stu_no = '{$stu_get}'";
$result = $pdo -> query($sql);
$stu_arr = $result -> fetch(PDO::FETCH_ASSOC);
// print_r($stu_arr);
?>
<br>
<h3>修改学生信息</h3>
<!-- 1.输入框设置 -->
<form action="" method="post">
<table>
<tr>
<td>学号:</td>
<td><input type="text" name="stu_no" value="$stu_arr['stu_no'] ?>"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="stu_name" value="$stu_arr['stu_name'] ?>"></td>
</tr>
<tr>
<td>姓别:</td>
<td>
男:<input type="radio" name="gender" value="男" <?php
if($stu_arr['gender']=='男'){
echo 'checked';
}
?>>
女:<input type="radio" name="gender" value="女"<?php
if($stu_arr['gender']=='女'){
echo 'checked';
}
?>>
</td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="telephone" value="$stu_arr['telephone'] ?>"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" value="$stu_arr['age'] ?>"></td>
</tr>
<tr>
<td>学院:</td>
<td><input type="text" name="college" value="$stu_arr['college'] ?>"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="修改">
<input type="reset" name="reset" value="重置">
</td>
</tr>
</table>
</form>
<?php
print_r($_SERVER["REQUEST_METHOD"]);//返回当前浏览器的请求方式
if($_SERVER["REQUEST_METHOD"] === 'POST'){
// print_r($_POST);
$stu_no = $_POST['stu_no'];
$stu_name = $_POST['stu_name'];
$gender = $_POST['gender'];
$telephone = $_POST['telephone'];
$age = $_POST['age'];
$college = $_POST['college'];
}
$sql = "UPDATE stu set stu_no='{$stu_no}',stu_name='{$stu_name}',
gender='{$gender}',telephone='{$telephone}',age='{$age}',college='{$college}' where stu_no='{$stu_get}'";
$result = $pdo -> exec($sql);
print_r($result);
if($result > 0){
header('Location:index.php');
}else{
echo '修改失败';
}
?>
</center>
</body>
</html>
<!-- 添加页面 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>删除信息页面</title>
</head>
<body>
<?php
require 'common/conn.php';
$stu_get = $_GET['stu_no'];
print_R($stu_get);
$sql = "DELETE from stu where stu_no='{$stu_get}'";
$result = $pdo->exec($sql);
header('Location/index.php');
?>
</body>
</html>