溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于PHP怎么實(shí)現(xiàn)原生增刪改查

發(fā)布時(shí)間:2022-05-05 13:51:09 來源:億速云 閱讀:135 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“基于PHP怎么實(shí)現(xiàn)原生增刪改查”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“基于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、列表頁(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ù)組輸出,不然會(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ù)庫的字符集
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怎么實(shí)現(xiàn)原生增刪改查

基于PHP怎么實(shí)現(xiàn)原生增刪改查

基于PHP怎么實(shí)現(xiàn)原生增刪改查

基于PHP怎么實(shí)現(xiàn)原生增刪改查

感謝各位的閱讀,以上就是“基于PHP怎么實(shí)現(xiàn)原生增刪改查”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對基于PHP怎么實(shí)現(xiàn)原生增刪改查這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI