基于PHP實現(xiàn)原生增刪改查的示例代碼
更新時間:2022年04月30日 10:52:32 作者:PHP開源社區(qū)
這篇文章主要介紹了如何利用php寫個原生增刪改查功能,文中的示例代碼講解,對我們學(xué)習(xí)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 -- 服務(wù)器版本: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` -- -- -------------------------------------------------------- -- -- 表的結(jié)構(gòu) `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; -- -- 轉(zhuǎn)存表中的數(shù)據(jù) `search` -- INSERT INTO `search` (`id`, `content`, `type`) VALUES (32, 'aaa', '特步'), (33, '陳業(yè)貴喜歡安踏', '安踏'); -- -------------------------------------------------------- -- -- 表的結(jié)構(gòu) `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; -- -- 轉(zhuǎn)存表中的數(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`); -- -- 在導(dǎo)出的表使用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里面的某一段值相對應(yīng)的就行了,就可以輸出啦
$result=mysqli_query($link,$sql);//運(yùn)行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里面的某一段值相對應(yīng)的就行了,就可以輸出啦
$result=mysqli_query($link,"DELETE FROM search WHERE id ='$_GET[id]'");//運(yùn)行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里面的某一段值相對應(yīng)的就行了,就可以輸出啦
$result=mysqli_query($link,"UPDATE search set content='$_GET[content]',type='$_GET[type]' WHERE id ='$_GET[id]'");//運(yùn)行sql
$sql="select * from search";
//模糊查詢出像數(shù)據(jù)庫中的title或者content里面的值或者說像數(shù)據(jù)庫中的title或者content里面的某一段值相對應(yīng)的就行了,就可以輸出啦
$result=mysqli_query($link,$sql);//運(yùn)行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)原生增刪改查的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于PHP 增刪改查的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用PHP函數(shù)進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化的代碼示例
在 PHP 中,有許多內(nèi)置函數(shù)和工具可以幫助我們實現(xiàn)網(wǎng)站的性能監(jiān)控和優(yōu)化,本文將介紹幾種常用的 PHP 函數(shù),并提供相應(yīng)的代碼示例,來幫助您更好地進(jìn)行網(wǎng)站性能監(jiān)控和優(yōu)化,需要的朋友可以參考下2024-06-06
php生成隨機(jī)數(shù)/生成隨機(jī)字符串的方法小結(jié)【5種方法】
這篇文章主要介紹了php生成隨機(jī)數(shù)/生成隨機(jī)字符串的方法,結(jié)合實例形式分析了php生成隨機(jī)數(shù)/生成隨機(jī)字符串的5種實現(xiàn)方法與相關(guān)操作注意事項,需要的朋友可以參考下2020-05-05
用PHP將數(shù)據(jù)導(dǎo)入到Foxmail的實現(xiàn)代碼
下面的原理就是用PHP生成一個文件,然后下載并把這些資料導(dǎo)入他們的Foxmail地址簿中。2010-09-09

