最近因为工作原因需要使用php开发网页,所以开始学习php,在学习的过程中也遇到了很多困难,经过不断的查询百度各种学习资料,逐步的客服了这些困难和疑惑,现在我将学习过程中编写的代码分享给有需要的朋友,仅供参考。
此系统比较简单,php页面一共9个,分别为conn.php(数据库连接),index.php(首页),add.php(添加记录页面),saveadd.php(添加记录后台),detail.php(成绩查询页面),update.php(个人成绩修改页面),saveupdate.php(个人成绩修改后台),del.php(个人成绩删除页面),savedel.php(个人成绩删除后台);css样式表一个;sql数据库导出文件一个;项目文件夹名称为ScoreQuery,下级目录为css,sql和9个php页面文件。此项目源码我已经上传至《资源下载》,连接地址为(https://download.csdn.net/download/modern358/11064474)请自行查找下载。
下面我对代码分别说明:
1.数据库
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- 主机: 127.0.0.1
-- 生成日期: 2019-03-27
-- 服务器版本: 10.1.37-MariaDB
-- PHP 版本: 7.3.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- 数据库: `phpweb`
--
CREATE DATABASE IF NOT EXISTS `phpweb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `phpweb`;
-- --------------------------------------------------------
--
-- 表的结构 `websql`
--
--
CREATE TABLE `websql` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`age` tinyint(4) DEFAULT NULL,
`result` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 表的关系 `websql`:
--
--
-- 转存表中的数据 `websql`
--
INSERT INTO `websql` (`id`, `name`, `age`, `result`) VALUES
(2, '测试2', 2, '2'),
(4, '测试4', 4, '4'),
(5, '测试5', 5, '5'),
(6, '测试6', 6, '6'),
(8, 'test8', 23, '87'),
(9, 'test9', 24, '69'),
(11, 'zhang1', 33, '99'),
(12, 'li2', 23, '98'),
(13, 'li1', 34, '78');
--
-- 转储表的索引
--
--
-- 表的索引 `websql`
--
ALTER TABLE `websql`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `websql`
--
ALTER TABLE `websql`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
此数据库导出文件中,有用的部分就是创建数据库CREATE DATABASE phpweb,和建立数据表websql,以及插入数据INSERT INTO websql.
2.index.php(首页)
<html>
<head>
<title>PHP成绩查询系统</title>
<link rel="stylesheet" type="text/css" href="css/table.css">
<link rel="stylesheet" type="text/css" href="css/center.css">
</head>
<body>
<table class="table1">
<caption>全部成绩查询列表</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
<th>操作</th>
</tr>
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
require_once ('conn.php');
$sql1 = mysqli_query($conn,"SELECT * FROM websql ORDER BY id ASC LIMIT 20");
while($info1=mysqli_fetch_array($sql1))
{
echo "<tr>";
echo "<td>".$info1['id']."</td>";
echo "<td>".$info1['name']."</td>";
echo "<td>".$info1['age']."</td>";
echo "<td>".$info1['result']."</td>";
echo "<td><a href='detail.php?id=".$info1['id']."'>详情</a></td>";
echo "</tr>";
}
?>
</table>
<div class="divbanner"><a href="add.php">添加一条记录</a></div>
</body>
</html>
3.add.php(添加记录页面)
<html>
<head>
<title>PHP成绩查询系统</title>
<link rel="stylesheet" href="css/table.css">
<link rel="stylesheet" type="text/css" href="css/center.css">
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
?>
<script type="text/javascript">
function chkinput(form)
{
if(form.name1.value == "")
{
form.name1.style.backgroundColor = "red";
form.name1.select();
return false;
}
if(form.age.value == "")
{
form.age.style.backgroundColor = "red";
form.age.select();
return false;
}
if(form.result.value =="")
{
form.result.style.backgroundColor = "red";
form.result.select();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="saveadd.php" onsubmit="return
chkinput(this)">
<table class="table2">
<caption>添加个人记录</caption>
<tr>
<td class="align_right">姓名:</td>
<td class="align_left"><input type="text" name="name1" id="name1"></td>
</tr>
<tr>
<td class="align_right">年龄:</td>
<td class="align_left"><input type="text" name="age" id="age"></td>
</tr>
<tr>
<td class="align_right">成绩:</td>
<td class="align_left"><input type="text" name="result" id="result"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="提 交">
<input type="reset" name="button2" id="button2" value="重 置"></td>
</tr>
</table>
</form>
<div class="divbanner"><a href="index.php">返回首页</a></div>
</body>
</html>
4.saveadd.php(添加记录后台)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
include ("conn.php");
$name = $_POST['name1'];
$age = $_POST['age'];
$result = $_POST['result'];
mysqli_query($conn,"INSERT INTO websql (name,age,result) VALUES('$name','$age','$result')");
echo "<script>alert('添加成功。');history.back();</script>";
?>
5.detail.php(成绩查询页面)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
require_once ("conn.php");
?>
<html>
<head>
<title>PHP成绩查询系统</title>
<link rel="stylesheet" href="css/table.css">
<link rel="stylesheet" href="css/center.css">
</head>
<body class = "ca">
<table class="table1">
<caption>个人成绩查询列表</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
<th>操作</th>
</tr>
<?php
$ID = $_GET['id'];
$sql = mysqli_query($conn,"SELECT * FROM websql WHERE id='".$ID."'");
while($info1=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>".$info1['id']."</td>";
echo "<td>".$info1['name']."</td>";
echo "<td>".$info1['age']."</td>";
echo "<td>".$info1['result']."</td>";
echo "<td><a href='update.php?id=".$info1['id']."'>更新</a>
<a href='del.php?id=".$info1['id']."'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
6.update.php(个人成绩修改页面)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
require_once ("conn.php");
?>
<html>
<head>
<title>PHP成绩查询系统</title>
<link rel="stylesheet" href="css/table.css">
<link rel="stylesheet" href="css/center.css">
<script type="text/javascript">
function chkinput(form)
{
if(form.name1.value == "")
{
form.name1.style.backgroundColor = "red";
form.name1.select();
return false;
}
if(form.age1.value == "")
{
form.age1.style.backgroundColor = "red";
form.age1.select();
return false;
}
if(form.result1.value =="")
{
form.result1.style.backgroundColor = "red";
form.result1.select();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form1" method="post" action="saveupdate.php" onsubmit="return chkinput(this)">
<table class="table1">
<caption>更新个人成绩列表</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
<th>操作</th>
</tr>
<?php
$ID = $_GET['id'];
$sql = mysqli_query($conn,"SELECT * FROM websql WHERE id='".$ID."'");
while($info1=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>".$info1['id']."</td>";
echo "<input type='hidden' name='id1' id='id1' value='".$info1['id']."' size='16'>";
echo "<td><input type='text' name='name1' id='name1' value='".$info1['name']."' size='16'></td>";
echo "<td><input type='text' name='age1' id='age1' value='".$info1['age']."' size='16'></td>";
echo "<td><input type='text' name='result1' id='result1' value='".$info1['result']."' size='16'></td>";
echo "<td> </td>";
echo "</tr>";
}
?>
<tr>
<td colspan="5" class="aline_center"><input type="submit" name="button" id="button" value="更新">
<input type="reset" name="button2" id="button2" value="重 置"></td>
</tr>
</table>
<div class="divbanner"><a href="index.php">返回首页</a></div>
</form>
</body>
</html>
7.saveupdate.php (个人成绩修改后台)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
$ID = $_POST['id1'];
$name = $_POST['name1'];
$age = $_POST['age1'];
$result = $_POST['result1'];
include ('conn.php');
mysqli_query($conn,"UPDATE websql SET name='$name',age='$age',result='$result' WHERE id='$ID'");
echo "<script>alert('修改成功。');</script>";
header("location:index.php");
?>
8.del.php(个人成绩删除页面)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
require_once ('conn.php');
?>
<html>
<head>
<title>PHP成绩查询系统</title>
<link rel="stylesheet" href="css/table.css">
<link rel="stylesheet" href="css/center.css">
</head>
<body>
<form name="form1" method="post" action="savedel.php">
<table class="table1">
<caption>删除个人成绩列表</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
<th>操作</th>
</tr>
<?php
$ID = $_GET['id'];
$sql = mysqli_query($conn,"SELECT * FROM websql WHERE id='".$ID."'");
while($info1=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>".$info1['id']."</td>";
echo "<input type='hidden' name='id1' id='id1' value='".$info1['id']."' size='16'>";
echo "<td>".$info1['name']."</td>";
echo "<td>".$info1['age']."</td>";
echo "<td>".$info1['result']."</td>";
echo "<td> </td>";
echo "</tr>";
}
?>
<tr>
<td colspan="5" class="aline_center"><input type="submit" name="button" id="button" value="确认删除"></td>
</tr>
</table>
</form>
<div class="divbanner"><a href="index.php">返回首页</a></div>
</body>
</html>
9.savedel.php(个人成绩删除后台)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
include ('conn.php');
$ID = $_POST['id1'];
mysqli_query($conn,"DELETE FROM websql WHERE id='$ID'");
echo "<script>alert('修改成功。');</script>";
header("location:index.php");
?>
10.css
目录为css\center.css,css\table.css。
<center.css>
.aline_center
{
text-align: center;
}
.ca
{
text-align: center;
}
.divbanner
{
text-align: center;
margin: 3px auto;
width: 800px;
height: 25px;
background-color: #cccccc;
border: 1px solid black;
}
.divbanner a
{
text-decoration: none;
color: #161f3f;
}
<table.css>
.table1
{
width: 800px;
margin: 3px auto;
border: 1px solid black;
border-collapse: collapse;
text-align: center;
color: #333333;
}
.table1 th,.table1 td
{
border: 1px solid black;
height: 30px;
line-height: 30px;
}
.table1 td
{
width: 20%;
}
.table1 caption
{
font-size: 25px;
margin-bottom: 25px;
margin-top: 50px;
font-weight: bold;
color: #3a902d;
}
/*------------------------------------------------------------------------------------------*/
.table2
{
width: 400px;
margin: 3px auto;
border: 1px solid black;
border-collapse: collapse;
text-align: center;
color: #333333;
}
.table2 th,.table2 td
{
border: 1px solid black;
height: 36px;
line-height: 36px;
}
.table2 td
{
width: 50%;
}
.table2 caption
{
font-size: 25px;
margin-bottom: 25px;
margin-top: 50px;
font-weight: bold;
color: #3a902d;
}
.table2 .align_left
{
text-align: left;
text-indent: 20px;
}
.table2 .align_right
{
text-align: right;
padding-right: 20px;
}
.table2 input[type=text]
{
height: 30px;
line-height: 30px;
}
11.conn.php(数据库连接)
<?php
/**
* Created by PhpStorm.
* User: jiawei
* Date: 2019/3/23
*/
//建立数据库连接
$conn = mysqli_connect("localhost","root","","phpweb");
@ mysqli_set_charset($conn,utf8);
@ mysqli_query($conn,utf8);
if(mysqli_connect_errno($conn))
{
echo "连接MySql失败".mysqli_connect_error();
}
?>