做了一个小系统,用原生php开发,采取b/s结构,主要体现的是php和数据库的增删改查,基本的界面如下,增删改查和涉及一些简单的字符串操作。
前端UI设计
前端UI用了一些bootstrap框架,采用的是jsDelivr,即一个免费且开源的CDN。
bootstrap网址:https://v4.bootcss.com/docs/getting-started/introduction/
css样式,就是那个背景图
.cotn_principal {
position: absolute;
width: 100%;
height: 100%;
background: #aac4bc;
background: -webkit-linear-gradient(-45deg, #aac4bc 0%, #eca8a8 100%, #eed5a9 100%);
background: linear-gradient(135deg, #aac4bc 0%, #eca8a8 100%, #eed5a9 100%);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr=\'#cfd8dc\', endColorstr=\'#b0bec5\', GradientType=1);
}
代码结构
由于用的是最原生的php,没什么代码结构,直接放在文件夹里调用哪个就取哪个就行,没什么技术含量,用的是vscode
数据库
设计了两个数据库,一个用于注册登录,一个用于另一个的增删改查,用的数据库工具是Navicat for Mysql
tm_user表
tm_word表
数据库创建语句
DROP TABLE IF EXISTS `tm_user`;
CREATE TABLE `tm_user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(158) DEFAULT NULL,
`password` varchar(158) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `tm_word`;
CREATE TABLE `tm_word` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(60) DEFAULT NULL,
`chara` varchar(10) DEFAULT NULL,
`chinese` varchar(255) DEFAULT NULL,
`voice` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`userLogin` varchar(255) DEFAULT NULL,
`userBian` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;
数据库链接,根据你的需要改成你的地址和账号密码就行
见php文件下的conn.php
<?php
$link = mysqli_connect(\'localhost\', \'root\', \'123456\', \'zTMtest\');
mysqli_set_charset($link, \'utf8\');
注册登录
写了注册登录功能,也是服务器端,后端链接数据库增删改查就行
登录的前端设计如下图
注册的前端设计如下图
涉及到代码也就四个,全用的php写的
登录代码如下:
login.php
<?php
session_start();
header("Content-Type:text/html;charset=utf-8");
?>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
<link href="../css/B.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../css/public.css">
</head>
<body>
<div class="cotn_principal">
<div class="box">
<h2>登录</h2>
<form action="check.php" method="post" enctype="multipart/form-data">
<div class="inputBox">
<input type="text" name="name" value="" required="required" placeholder=
" 请输入您的帐号" title="学号为13为数字"><label></label></div>
<div class="inputBox">
<input type="password" name="password" value="" required="required" placeholder=
" 请输入您的密码"><label></label></div>
<input type="submit" name="submit" value="登录" >
<input type="button" onclick="window.location.href=\'loginup.php\'" name="submit" value="注册" >
</form>
</div>
</div>
</body>
</html>
check.php
<?php
/*登录数据库验证 */
include \'conn.php\';
//include \'sign.php\';
$name=$_POST[\'name\'];
$password=$_POST[\'password\'];
//前面的name代表数据库中的name字段
if($name && $password)
{
$sql = "SELECT * FROM tm_user where name=\'$name\'and password=\'$password\'";
$result = mysqli_query($link, $sql);
$num=mysqli_fetch_row($result);
if($num){
echo"<script type=\'text/javascript\'>alert(\'登陆成功\');location=\'../index.php\';</script>";
}
else{
//echo"该用户不存在或者账号密码错误";
echo"<script type=\'text/javascript\'>alert(\'该用户不存在或者账号密码错误\');location=\'login.php\';</script>";
}
}
else{
echo"用户名或密码为空";
}
mysqli_close($link);
?>
注册代码如下,涉及数据库预处理的语法
loginup.php
<?php
header("Content-Type:text/html;charset=utf-8");
?>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
<link href="../css/B.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../css/public.css">
</head>
<body>
<div class="cotn_principal">
<div class="box">
<h2>注册</h2>
<!--将用户输入的user,和pass提交到login.php-->
<form action="checkregister.php" method="POST" enctype="multipart/form-data">
<div class="inputBox"><input type="text" name="name" value="" required="required"
placeholder= "请输入您的用户名" ><label></label></div>
<div class="inputBox"><input type="password" name="password" value="" required="required"
placeholder="请输入您的密码"><label></label></div>
<div class="inputBox"><input type="password" name="pass" value="" required="required"
placeholder="请重复您的密码"><label></label></div>
<input type="submit" name="submit" value="确认注册">
<input type="button" onclick="window.location.href=\'login.php\'" value="返回登陆">
</form>
</div>
</div>
</body>
</html>
checkregister.php,里面涉及预处理的函数,用的是myqli
<?php
include \'conn.php\';
//include \'register.php\';
$name=$_POST[\'name\'];
$password=$_POST[\'password\'];
$pass=$_POST[\'pass\'];
if($password!=$pass){
echo "<script>alert(\'两次输入密码不一致!\');location=\'loginup.php\'</script>";
}
//使用mysql预处理语句尝试完成数据库插入操作
if($password==$pass){
$sql="INSERT INTO tm_user(name,password) VALUES(?, ?) ";
$stmt = $link->prepare($sql);
//这个s和后面绑定的字符段数量对应
$stmt->bind_param("ss", $name, $password);
$result=$stmt->execute();
if($result){
echo "<script>alert(\'注册成功!\');location=\'login.php\'</script>";
}
else{
echo "<script>alert(\'注册失败!\');location=\'loginup.php\'</script>";
}
}
// $stmt->close();
// $link->close();
?>
登录成功后也就能进入到主页面index.php,也就是第一张图,数据是从数据库读然后输出的
<?php
include \'php/conn.php\';
//查询数量
$sql_count = \'SELECT COUNT(*) FROM `tm_word`\' ;
$n = mysqli_query($link, $sql_count);
if (!$n) {
// 获取错误信息
exit(\'查询数量sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$num = mysqli_fetch_assoc($n);
$records = implode($num);
//查询语句
$sql = \'SELECT * FROM `tm_word`\';
$result = mysqli_query($link, $sql);
if (!$result) {
// 获取错误信息
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
// echo $num;
?>
<!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">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<title>Document</title>
<link rel="stylesheet" href="css/public.css">
</head>
<body>
<div class="cotn_principal">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid container">
<div class="navbar-header">
<a class="navbar-brand" href="main.html">英文参照助手</a>
</div>
<div class="float-right ">
<ul class="nav navbar-nav navbar-right">
<li><a href="php/chaxun.php">搜索</a></li>
<li><a href="php/translate.php">翻译</a></li>
<li class="active"><a href="html/addword.html">录入英文</a></li>
</ul>
</div>
</nav>
<div class="container">
<table class="table table-hover">
<caption>英文参照助手,共有<?php echo $records;?>条数据</caption>
<thead>
<tr>
<th>英文</th>
<th>词性</th>
<th>中文</th>
<th>音标</th>
<th>级别</th>
<th>录入人员</th>
<th>编辑人员</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
echo "<tr>";
echo "<td>{$arr[\'word\']}</td>";
echo "<td>{$arr[\'chara\']}</td>";
echo "<td>{$arr[\'chinese\']}</td>";
echo "<td>{$arr[\'voice\']}</td>";
echo "<td>{$arr[\'type\']}</td>";
echo "<td>{$arr[\'userLogin\']}</td>";
echo "<td>{$arr[\'userBian\']}</td>";
echo "<td>
<a href =\'./php/edit.php?id={$arr[\'id\']}\'>编辑</a>
<a href =\'./php/deleteword.php?id={$arr[\'id\']}\'>删除</a>
</td>
";
echo "</tr>";
}
mysqli_close($link);
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
添加数据库操作
操作的数据库是tm_word,添加到网页在html文档里的addword.html,对数据库操作在php的addword.php,先展示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">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../css/public.css">
<title>Document</title>
</head>
<body>
<div class="cotn_principal">
<!-- 导航栏 -->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid container">
<div class="navbar-header">
<a class="navbar-brand" href="">录入英文</a>
</div>
<div class="float-right ">
<ul class="nav navbar-nav navbar-right">
<li><a href="../php/chaxun.php">搜索</a></li>
<li class="active"><a href="../index.php">英文参照助手</a></li>
</ul>
</div>
</nav>
<!-- 添加单词表单 -->
<div class="d-flex justify-content-center">
<div class="container">
<!-- action="../php/addword.php" -->
<form action="../php/addword.php" method="post" enctype="multipart/form-data" id=\'myform\' name=\'myform\'>
<table>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英文:</label>
<input type="text" class="form-control" id="word" placeholder="输入你要添加到英文,不可省略!" name="word">
</div>
<!-- <div id="validate3" style="color: brown;">必填项</div> -->
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlSelect1">单词词性:</label>
<select class="form-control" id="" name="chara">
<option></option>
<option>n</option>
<option>v</option>
<option>vt</option>
<option>vi</option>
<option>adj</option>
<option>pron</option>
</select>
</div>
<!-- <div id="validate2" style="color:skyblue;">选填项</div> -->
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">中文翻译:</label>
<input type="text" class="form-control" id="chinese" placeholder="输入你要添加到中文意思,不可省略!" name="chinese">
</div>
<!-- <div id="validate3" style="color: brown;">必填项</div> -->
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英语音标:</label>
<input type="text" class="form-control" id="" placeholder="[ˈæpl]" name="voice">
</div>
<!-- <div id="validate2" style="color:skyblue;">选填项</div> -->
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlSelect1">级别:</label>
<select class="form-control" id="" name="type">
<option></option>
<option>common</option>
<option>professonal</option>
<option>New</option>
<option>Confirmed</option>
<option>Machine Translated</option>
</select>
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">录入人员帐户名:</label>
<input type="text" class="form-control" id="" placeholder="" name="userLogin">
</div>
</tr>
<tr>
<button type="submit" class="btn btn-primary" onclick="checkForm();">提交</button>
<button type="button" class="btn btn-outline-info" id="" onclick="resetForm();">重置</button>
<button type="button" class="btn btn-outline-dark" id="" onClick="javascript :history.back(-1);">返回</button>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
<script type="text/javascript">
// 初始化
function $(id) {
return document.getElementsById(id);
}
// 判断字符串是否为空
function isEmpty(str) {
if (str == null || str.trim() == "") {
return true;
} else {
return false;
}
}
//重置表单
function resetForm() {
$("myform").reset();
}
function checkForm() {
var word = document.getElementById("word").value;
var chinese = document.getElementById("chinese").value;
//if (word == \'\') {
if (isEmpty(word)) {
//document.getElementById("validate1").innerHTML = "word不可空!";
alert("word不可空!");
return;
}
if (isEmpty(chinese)) {
alert("中文不可空!");
return;
}
}
</script>
</html>
addword.php
<?php
include \'conn.php\';
//获取客户端信息
$word=$_POST[\'word\'];
$chara=$_POST[\'chara\'];
$chinese=$_POST[\'chinese\'];
$voice=$_POST[\'voice\'];
$type=$_POST[\'type\'];
$userLogin=$_POST[\'userLogin\'];
//查询语句
$sql = " SELECT * FROM tm_word where word=\'{$word}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($data as $key => $value) {
foreach ($value as $k => $v) {
$arr[$k]=$v;
}
// echo $arr["id"];
// echo $arr["word"];
}
if(!empty($arr["id"])){
//输入数据重复
echo "<script>alert(\'你输入的数据已存在!\');location=\'../html/addword.html\';</script>";
exit();
}
if(!empty($word) && !empty($chinese)){
$sql1 = " INSERT INTO tm_word(word,chara,chinese,voice,type,userLogin) VALUES(?,?,?,?,?,?) ";
$stmt = $link -> prepare($sql1);
$stmt->bind_param(\'ssssss\', $word, $chara, $chinese,$voice,$type,$userLogin);
$result1=$stmt->execute();
if($result1){
echo "<script>alert(\'添加成功!\');location=\'../html/addword.html\';</script>";
}else{
exit(\'添加学生sql语句执行失败。错误信息:\' . mysqli_error($link));
}
}
header("../index.php");
添加页面如下图
编辑操作
编辑操作涉及到是php文件夹下的edit.php(包含前端)和updateword.php(处理数据)
edit.php
<?php
error_reporting(0);
include \'conn.php\';
//获取id
$id=$_GET[\'id\'];
$sql = "SELECT * FROM tm_word WHERE id={$id} ";
$result = mysqli_query($link, $sql);
// if (!$result) {
// exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
// }
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($data as $key => $value) {
foreach ($value as $k => $v) {
$arr[$k]=$v;
}
}
?>
<!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">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../css/public.css">
<title>Document</title>
</head>
<body>
<div class="cotn_principal">
<!-- 重新布局 -->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid container">
<div class="navbar-header">
<a class="navbar-brand" href="main.html">编辑</a>
</div>
<div class="float-right ">
<ul class="nav navbar-nav navbar-right">
<li><a href="../html/addword.html">添加</a></li>
<li class="active"><a href="../index.php">英文表</a></li>
</ul>
</div>
</nav>
<!-- 输出单词列表 -->
<div class="d-flex justify-content-center">
<div class="container">
<!-- action="../php/addword.php" -->
<form action="updateword.php" method="post" enctype="multipart/form-data" id=\'myform\' name=\'myform\'>
<table>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">id:</label>
<input type="text" class="form-control" placeholder="" name="id" value="<?php echo $arr["id"];?>">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英文单词:</label>
<input type="text" class="form-control" id="word" placeholder="" name="word" value="<?php echo $arr["word"];?>">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">词性:</label>
<input type="text" class="form-control" id="word" placeholder="" name="chara" value="<?php echo $arr["chara"];?>">
</div>
</tr>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">中文翻译:</label>
<input type="text" class="form-control" id="" placeholder="" name="chinese" value="<?php echo $arr["chinese"];?>">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英语音标:</label>
<input type="text" class="form-control" id="" placeholder="" name="voice" value="<?php echo $arr["voice"];?>" >
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlSelect1">级别:</label>
<select class="form-control" id="" name="type">
<option></option>
<option>common</option>
<option>professonal</option>
<option>New</option>
<option>Confirmed</option>
<option>Machine Translated</option>
</select>
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">编辑人员账户名:</label>
<input type="text" class="form-control" id="" placeholder="" name="userBian" value="<?php echo $arr["userBian"];?>" >
</div>
</tr>
<tr>
<button type="submit" class="btn btn-primary" >提交</button>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
updateword.php中涉及一些预处理sql语句
<?php
error_reporting(0);
include \'conn.php\';
$id =$_POST[\'id\'];
$word=$_POST[\'word\'];
$chara=$_POST[\'chara\'];
$chinese=$_POST[\'chinese\'];
$voice=$_POST[\'voice\'];
$type=$_POST[\'type\'];
$userBian=$_POST[\'userBian\'];
$sql = "update tm_word set Word=?,Chara=?,Chinese=?,Voice=?,Type=?,UserBian=? where Id=?";
$stmt = $link -> prepare($sql);
$stmt->bind_param(\'sssssss\', $word, $chara, $chinese, $voice,$type,$userBian,$id);
$result=$stmt->execute();
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
else{
echo "<script>alert(\'编辑成功!\');location=\'edit.php\';</script>";
}
mysqli_close($link);
录入操作的前端页面如下图
删除操作
就一个delectword.php,在index.php采取的是get方法
<?php
include \'conn.php\';
$id=$_GET[\'id\'];
$sql = "DELETE FROM tm_word WHERE id={$id}";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询数据sql语句执行失败。错误信息:\'.mysqli_error($link));
// 获取错误信息
}
//header("Location:index.php");
查询
根据输入的关键字,在已录入的数据中搜索对应词条并展示:模糊/匹配查询,就一个chaxun.php
<?php
error_reporting(0);
?>
<!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">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../css/public.css">
<title>搜索</title>
</head>
<body>
<div class="cotn_principal">
<!-- 导航栏 -->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid container">
<div class="navbar-header">
<a class="navbar-brand" href="">搜索单词</a>
</div>
<div class="float-right ">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="../html/addword.html">添加</a>
</li>
<li class="active"><a href="../index.php">单词表</a></li>
</ul>
</div>
</nav>
<nav aria-label="breadcrumb">
<div class="d-flex justify-content-center">
<div class="container">
<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英文:</label>
<input type="text" class="form-control" id="" placeholder="" name="word">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlSelect1">词性:</label>
<select class="form-control" id="" name="chara">
<option></option>
<option>n</option>
<option>v</option>
<option>vt</option>
<option>vi</option>
<option>adj</option>
<option>pron</option>
</select>
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">中文:</label>
<input type="text" class="form-control" id="" placeholder="" name="chinese">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">音标:</label>
<input type="text" class="form-control" id="" placeholder="" name="voice">
</div>
</tr>
<tr>
<div class="form-group">
<label for="exampleFormControlSelect1">级别:</label>
<select class="form-control" id="" name="type">
<option></option>
<option>common</option>
<option>professonal</option>
<option>New</option>
<option>Confirmed</option>
<option>Machine Translated</option>
</select>
</div>
</tr>
<tr>
<button type="submit" class="btn btn-primary">搜索</button>
</tr>
</table>
</form>
</div>
</div>
</nav>
<?php
$word=$_POST[\'word\'];
$chara=$_POST[\'chara\'];
$chinese=$_POST[\'chinese\'];
$voice=$_POST[\'voice\'];
$type=$_POST[\'type\'];
// echo $word;
// echo $chara;
// echo $chinese;
// echo $voice;
//echo $type;
include \'conn.php\';
if(!empty($word)){
//封装模糊查询 赋值到数组
$where = \'%\'.$word.\'%\';
$sql = "SELECT * FROM tm_word WHERE `word` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
if(!empty($chara)){
//封装模糊查询 赋值到数组
// $where = \'%\'.$chara.\'%\';
$where = $chara;
$sql = "SELECT * FROM tm_word WHERE `chara` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
if(!empty($chinese)){
//封装模糊查询 赋值到数组
$where = \'%\'.$chinese.\'%\';
$sql = "SELECT * FROM tm_word WHERE `chinese` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
if(!empty($voice)){
//封装模糊查询 赋值到数组
$where = \'%\'.$voice.\'%\';
$sql = "SELECT * FROM tm_word WHERE `voice` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
if(!empty($type)){
//封装模糊查询 赋值到数组
// $where = \'%\'.$chara.\'%\';
$where = $type;
$sql = "SELECT * FROM tm_word WHERE `type` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
?>
<!-- 输出列表 -->
<div class="container">
<table class="table table-hover">
<caption>符合搜索条件的英文,共有<?php echo $records;?>条数据。</caption>
<thead>
<tr>
<th>word</th>
<th>词性</th>
<th>中文</th>
<th>音标</th>
<th>级别</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
echo "<tr>";
echo "<td>{$arr[\'word\']}</td>";
echo "<td>{$arr[\'chara\']}</td>";
echo "<td>{$arr[\'chinese\']}</td>";
echo "<td>{$arr[\'voice\']}</td>";
echo "<td>{$arr[\'type\']}</td>";
echo "</tr>";
}
mysqli_close($link);
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
运行结果如下
字符串操作
不是正规的,就是对字符串进行一些处理。
translate.php
<?php
error_reporting(0);
include \'conn.php\';
// 中文字符串转化成数组
function ch2length($str)
{
$length = mb_strlen($str, \'utf-8\');
return $length;
}
function ch2arr($str)
{
$length = mb_strlen($str, \'utf-8\');
$array = [];
for ($i=0; $i<$length; $i++)
$array[] = mb_substr($str, $i, 1, \'utf-8\');
return $array;
}
?>
<!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">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<title>Document</title>
<link rel="stylesheet" href="../css/public.css">
<style>
</style>
</head>
<body>
<div class="cotn_principal">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid container">
<div class="navbar-header">
<a class="navbar-brand" href="">翻译</a>
</div>
<div class="float-right ">
<ul class="nav navbar-nav navbar-right">
<li><a href="../index.php">英文参照助手</a></li>
<li><a href="../html/addword.html">添加英文</a></li>
<li><a href="../php/chaxun.php">搜索</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="container">
<form action="#" method="get" enctype="multipart/form-data">
<table>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">英文输入:</label>
<input type="text" class="form-control" id="word" placeholder="输入你要查询的单词!" name="word">
</div>
</tr>
<tr>
<button type="submit" class="btn btn-primary" onclick="checkForm();">提交</button>
</tr>
</table>
</form>
</div>
</div>
<?php
$word=$_GET[\'word\'];
$pieces = explode(" ", $word);
$ss="";
foreach($pieces as $key => $value){
//echo "key 为 " . $key . ",对应的 value 为 ". $value . PHP_EOL;
}
//echo $pieces[0];
//$num 数组数量
$num = count($pieces);
//查询语句
for($i=0;$i<$num;$i++){
$sql = "SELECT * FROM tm_word WHERE word=\'{$pieces[$i]}\' ";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
if (!$result) {
// 获取错误信息
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
else{
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
//echo "<td>{$arr[\'chinese\']}</td>";
}
$ss=$ss.$arr[\'chinese\'];
//echo $ss;
}
}
?>
<div class="container">
<div class="container">
<table>
<tr>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<p><?php echo $ss;?></p>
</div>
</div>
</tr>
</table>
</div>
</div>
<div class="container">
<div class="container">
<form action="#" method="get" enctype="multipart/form-data">
<table>
<tr>
<div class="form-group">
<label for="exampleFormControlInput1">中文输入:</label>
<input type="text" class="form-control" id="zhongwen" placeholder="输入你要查询的单词!" name="zhongwen">
</div>
</tr>
<tr>
<button type="submit" class="btn btn-primary" onclick="checkForm();">提交</button>
</tr>
</table>
</form>
</div>
</div>
<?php
$zhongwen=$_GET[\'zhongwen\'];
$ss="";
if(!empty($zhongwen)){
//封装模糊查询 赋值到数组
$where = \'%\'.$zhongwen.\'%\';
$sql = "SELECT * FROM tm_word WHERE `chinese` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$records=mysqli_num_rows($result);
}
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
//echo $arr[\'word\'];
}
if($records!=0){
$ss=$ss.$arr[\'word\'];
}
//echo $records;
else{
$arr1 = ch2arr($zhongwen);
$length=ch2length($zhongwen);
//print_r($arr1);
//echo $length."</br>";
for($i=0;$i<$length;$i++){
$j=$i;
$where = $arr1[$i];
$sql = "SELECT * FROM tm_word WHERE `chinese` LIKE \'{$arr1[$i]}\' ";
$result = mysqli_query($link, $sql);
if (!$result) {
exit(\'查询sql语句执行失败。错误信息:\'.mysqli_error($link));
}
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$ze_num=mysqli_num_rows($result);
if(!empty($ze_num)){
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
}
$ss=$ss.$arr[\'word\']." ";
}
if(empty($ze_num)){
$j++;
$where = $arr1[$i].$arr1[$j];
$sql = "SELECT * FROM tm_word WHERE `chinese` LIKE \'{$where}\' ";
$result = mysqli_query($link, $sql);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
$ze_num2=mysqli_num_rows($result);
if(!empty($ze_num2)){
foreach($data as $key => $value){
foreach($value as $k=>$v){
$arr[$k]=$v;
}
}
$ss=$ss.$arr[\'word\']." ";
$i++;
}
}
}
}
?>
<div class="container">
<div class="container">
<table>
<tr>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<p><?php echo $ss;?></p>
</div>
</div>
</tr>
</table>
</div>
</div>
</body>
</html>
参考文档
[1] https://gitee.com/anidea/Web