基于PHP實現(xiàn)原生增刪改查的示例代碼
更新時間:2022年04月30日 10:52:32 作者:PHP開源社區(qū)
這篇文章主要介紹了如何利用php寫個原生增刪改查功能,文中的示例代碼講解,對我們學習PHP有一定幫助,感興趣的小伙伴可以了解一下
一、代碼
1、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; -- -- 轉存表中的數(shù)據(jù) `search` -- INSERT INTO `search` (`id`, `content`, `type`) VALUES (32, 'aaa', '特步'), (33, '陳業(yè)貴喜歡安踏', '安踏'); -- -------------------------------------------------------- -- -- 表的結構 `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; -- -- 轉存表中的數(shù)據(jù) `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 */;
2、列表頁(index.php)
<?php $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql="select * from search"; //模糊查詢出像數(shù)據(jù)庫中的title或者content里面的值或者說像數(shù)據(jù)庫中的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)) {//把對象編程數(shù)組輸出,不然會報錯哦 # code... ?> <tr> <td><?=$row['id'];?></td> <td><?=$row['content'];?></td> <td><?=$row['type'];?></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> </tr> <?php } ?> </tr> </table> <a href="create.php" rel="external nofollow" >創(chuàng)建</a> </body> </html>
3、delete.php
<?php $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); //$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'"; //模糊查詢出像數(shù)據(jù)庫中的title或者content里面的值或者說像數(shù)據(jù)庫中的title或者content里面的某一段值相對應的就行了,就可以輸出啦 $result=mysqli_query($link,"DELETE FROM search WHERE id ='$_GET[id]'");//運行sql echo "<script>alert('恭喜你,刪除成功了');location.href='index.php';</script>"; ?>
4、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鏈接數(shù)據(jù)庫的字符集 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鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); //$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'"; //模糊查詢出像數(shù)據(jù)庫中的title或者content里面的值或者說像數(shù)據(jù)庫中的title或者content里面的某一段值相對應的就行了,就可以輸出啦 $result=mysqli_query($link,"UPDATE search set content='$_GET[content]',type='$_GET[type]' WHERE id ='$_GET[id]'");//運行sql $sql="select * from search"; //模糊查詢出像數(shù)據(jù)庫中的title或者content里面的值或者說像數(shù)據(jù)庫中的title或者content里面的某一段值相對應的就行了,就可以輸出啦 $result=mysqli_query($link,$sql);//運行sql echo "<script>alert('恭喜你,更新成功了');location.href='index.php';</script>"; ?>
5、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鏈接數(shù)據(jù)庫的字符集 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="創(chuàng)建"> </form> </body> </html> <?php if(!$_POST['content']||$_POST['type']==-1) { exit(); } $content=$_POST['content']; $type=$_POST['type']; $link=mysqli_connect('localhost','root','','a'); //然后是指定php鏈接數(shù)據(jù)庫的字符集 mysqli_set_charset($link,'utf8'); $sql = "INSERT INTO search(content,type) VALUES ('{$content}','{$type}')"; $result=mysqli_query($link,$sql); if($result) { echo "<script>alert('創(chuàng)建成功');location.href='index.php';</script>"; } ?>
二、效果圖
以上就是基于PHP實現(xiàn)原生增刪改查的示例代碼的詳細內容,更多關于PHP 增刪改查的資料請關注腳本之家其它相關文章!
相關文章
PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)的詳解
本篇文章是對PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP call_user_func和call_user_func_array函數(shù)的簡單理解與應用分析
這篇文章主要介紹了PHP call_user_func和call_user_func_array函數(shù)的簡單理解與應用,結合實例形式分析了PHP call_user_func和call_user_func_array函數(shù)的基本功能、用法及操作注意事項,需要的朋友可以參考下2019-11-11php5.3 不支持 session_register() 此函數(shù)已啟用的解決方法
php從5.2.x升級到5.3.2.出來問題了。有些原來能用的程序報錯了,Deprecated: Function session_register() is deprecated2013-11-11