php实现curd案例(改进版并提供全部源代码)
【摘要】
sql
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Gen...
sql
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-03-19 19:16:40
-- 服务器版本: 10.1.13-MariaDB
-- PHP Version: 5.6.21
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
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 */;
--
-- Database: `a`
--
-- --------------------------------------------------------
--
-- 表的结构 `search`
--
CREATE TABLE `search` (
`id` int(20) NOT NULL,
`content` text COLLATE utf8_vietnamese_ci NOT NULL,
`type` varchar(100) COLLATE utf8_vietnamese_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci;
--
-- 转存表中的数据 `search`
--
INSERT INTO `search` (`id`, `content`, `type`) VALUES
(32, 'aaa', '特步'),
(33, '陈业贵喜欢安踏', '安踏');
-- --------------------------------------------------------
--
-- 表的结构 `type`
--
CREATE TABLE `type` (
`id` int(11) NOT NULL,
`type` varchar(12) COLLATE utf8_vietnamese_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci;
--
-- 转存表中的数据 `type`
--
INSERT INTO `type` (`id`, `type`) VALUES
(1, '安踏'),
(2, '特步');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `search`
--
ALTER TABLE `search`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `type`
--
ALTER TABLE `type`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `search`
--
ALTER TABLE `search`
MODIFY `id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=34;
--
-- 使用表AUTO_INCREMENT `type`
--
ALTER TABLE `type`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
/*!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 */;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
列表页(index.php)
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql="select * from search";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql
?>
<!--显示的效果-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<td>id</td>
<td>种类</td>
<td>物品</td>
<?php
while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
# code...
?>
<tr>
<td><?=$row['id'];?></td>
<td><?=$row['content'];?></td>
<td><?=$row['type'];?></td>
<td><a href="update.php?id=<?php echo $row['id']; ?>">更新</a></td>
<td><a href="delete.php?id=<?php echo $row['id']; ?>">删除</a></td>
</tr>
<?php
}
?>
</tr>
</table>
<a href="create.php">创建</a>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
delete.php
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,"DELETE FROM search WHERE id ='$_GET[id]'");//运行sql
echo "<script>alert('恭喜你,删除成功了');location.href='index.php';</script>";
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
update.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="update.php" method="GET">
<input type="hidden" name="id" value="<?php echo $_GET['id']?>">
<td>
<select name='type' width='500' class='selectfont'>
<option value='-1'>请选择</option>
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql='select * from type';
$re = mysqli_query($link,$sql);
while($arr = mysqli_fetch_array($re)){
echo "<option value='".$arr['type']."'>".$arr['type']."</option>";
}
?>
</select>
<input type="text" name="content">
<input type="submit" value="修改">
</form>
</body>
</html>
<?php
if(!$_GET['content']||$_GET['type']==-1)
{
exit();
}
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,"UPDATE search set content='$_GET[content]',type='$_GET[type]' WHERE id ='$_GET[id]'");//运行sql
$sql="select * from search";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql
echo "<script>alert('恭喜你,更新成功了');location.href='index.php';</script>";
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
create.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="create.php" method="POST">
<input type="text" name="content">
<td>
<select name='type' width='500' class='selectfont'>
<option value='-1'>请选择</option>
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql='select * from type';
$re = mysqli_query($link,$sql);
while($arr = mysqli_fetch_array($re)){
echo "<option value='".$arr['type']."'>".$arr['type']."</option>";
}
?>
</select>
</td>
<input type="submit" value="创建">
</form>
</body>
</html>
<?php
if(!$_POST['content']||$_POST['type']==-1)
{
exit();
}
$content=$_POST['content'];
$type=$_POST['type'];
$link=mysqli_connect('localhost','root','','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql = "INSERT INTO search(content,type)
VALUES ('{$content}','{$type}')";
$result=mysqli_query($link,$sql);
if($result)
{
echo "<script>alert('创建成功');location.href='index.php';</script>";
}
?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
效果图:
文章来源: blog.csdn.net,作者:贵哥的编程之路(热爱分享),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/123606854
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)