PHP连接数据库实现页面增删改查效果
吾爱主题
阅读:190
2022-11-03 16:27:00
评论:0
效果图
实现代码
sql
?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 | -- phpMyAdmin SQL Dump -- version 4.5.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: 2022-03-15 17:51:32 -- 服务器版本: 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 (11) NOT NULL , `content` text COLLATE utf8_vietnamese_ci NOT NULL COMMENT '内容' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE =utf8_vietnamese_ci; -- -- 转存表中的数据 `search` -- INSERT INTO `search` (`id`, `content`) VALUES (21, '陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵' ), (22, '陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵' ), (23, '陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵' ), (24, '陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵陈作业贵' ), (25, '陈作业贵' ), (26, '陈作业贵' ), (27, '陈作业贵' ), (28, '陈作业贵' ), (29, '000000' ), (30, '' ); -- -- Indexes for dumped tables -- -- -- Indexes for table `search` -- ALTER TABLE `search` ADD PRIMARY KEY (`id`); -- -- 在导出的表使用AUTO_INCREMENT -- -- -- 使用表AUTO_INCREMENT `search` -- ALTER TABLE `search` MODIFY `id` int (11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67; /*!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 */; |
cyg.php
?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 | <?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><a href= "update.php?id=<?php echo $row['id']; ?>" rel= "external nofollow" >更新</a></td> <td><a href= "delete.php?id=<?php echo $row['id']; ?>" rel= "external nofollow" >删除</a></td> <td><a href= "create.php" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >创建</a></td> </tr> <?php } ?> </tr> </table> </body> </html> |
delete.php
?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 | <?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 $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><a href= "update.php?id=<?= $row['id']; ?>" rel= "external nofollow" rel= "external nofollow" >更新</a></td> <td><a href= "delete.php?id=<?= $row['id']; ?>" rel= "external nofollow" rel= "external nofollow" >删除</a></td> <td><a href= "create.php" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >创建</a></td> </tr> <?php } ?> </tr> </table> </body> </html> |
update.php
?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 | <!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']?>" > <input type= "text" name= "content" > <input type= "submit" value= "搜索" > </form> </body> </html> <?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 , "UPDATE search set content='$_GET[content]' WHERE id ='$_GET[id]'" ); //运行sql $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><a href= "update.php?id=<?= $row['id']; ?>" rel= "external nofollow" rel= "external nofollow" >更新</a></td> <td><a href= "delete.php?id=<?= $row['id']; ?>" rel= "external nofollow" rel= "external nofollow" >删除</a></td> <td><a href= "create.php" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >创建</a></td> </tr> <?php } ?> </tr> </table> </body> </html> |
create.php
?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 | <!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" > <input type= "submit" value= "提交" > </form> </body> </html> <?php if (! $_POST [ 'content' ]) { exit (); } $content = $_POST [ 'content' ]; $link =mysqli_connect( 'localhost' , 'root' , '' , 'a' ); //然后是指定php链接数据库的字符集 mysqli_set_charset( $link , 'utf8' ); $sql = "INSERT INTO search(content) VALUES ( '{$content}' )"; $result =mysqli_query( $link , $sql ); echo "<script>alert('创建成功');</script>" ; ?> |
以上就是PHP连接数据库实现页面增删改查效果的详细内容,更多关于PHP增删改查的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/qq_37805832/article/details/123516417
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。