基于PHP實(shí)現(xiàn)原生增刪改查的示例代碼
一、代碼
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、列表頁(yè)(index.php)
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php鏈接數(shù)據(jù)庫(kù)的字符集
mysqli_set_charset($link,'utf8');
$sql="select * from search";
//模糊查詢出像數(shù)據(jù)庫(kù)中的title或者content里面的值或者說像數(shù)據(jù)庫(kù)中的title或者content里面的某一段值相對(duì)應(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)) {//把對(duì)象編程數(shù)組輸出,不然會(huì)報(bào)錯(cuò)哦
# 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ù)庫(kù)的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";
//模糊查詢出像數(shù)據(jù)庫(kù)中的title或者content里面的值或者說像數(shù)據(jù)庫(kù)中的title或者content里面的某一段值相對(duì)應(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'>請(qǐng)選擇</option>
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php鏈接數(shù)據(jù)庫(kù)的字符集
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ù)庫(kù)的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";
//模糊查詢出像數(shù)據(jù)庫(kù)中的title或者content里面的值或者說像數(shù)據(jù)庫(kù)中的title或者content里面的某一段值相對(duì)應(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ù)庫(kù)中的title或者content里面的值或者說像數(shù)據(jù)庫(kù)中的title或者content里面的某一段值相對(duì)應(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'>請(qǐng)選擇</option>
<?php
$link=mysqli_connect('localhost','root','','a');
//然后是指定php鏈接數(shù)據(jù)庫(kù)的字符集
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ù)庫(kù)的字符集
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實(shí)現(xiàn)原生增刪改查的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于PHP 增刪改查的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)的詳解
本篇文章是對(duì)PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php+js實(shí)現(xiàn)的無刷新下載文件功能示例
這篇文章主要介紹了php+js實(shí)現(xiàn)的無刷新下載文件功能,結(jié)合實(shí)例形式分析了php無刷新下載文件的相關(guān)原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
又一個(gè)php 分頁(yè)類實(shí)現(xiàn)代碼
php 分頁(yè)類代碼,比較簡(jiǎn)單。2009-12-12
PHP call_user_func和call_user_func_array函數(shù)的簡(jiǎn)單理解與應(yīng)用分析
這篇文章主要介紹了PHP call_user_func和call_user_func_array函數(shù)的簡(jiǎn)單理解與應(yīng)用,結(jié)合實(shí)例形式分析了PHP call_user_func和call_user_func_array函數(shù)的基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
php5.3 不支持 session_register() 此函數(shù)已啟用的解決方法
php從5.2.x升級(jí)到5.3.2.出來問題了。有些原來能用的程序報(bào)錯(cuò)了,Deprecated: Function session_register() is deprecated2013-11-11

