博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
135行实现CRUD功能(PHP)
阅读量:2239 次
发布时间:2019-05-09

本文共 3684 字,大约阅读时间需要 12 分钟。

创建表语句:

1 
CREATE 
TABLE 
IF 
NOT 
EXISTS `task` (
2   `id` 
int(
11
NOT 
NULL AUTO_INCREMENT,
3   `name` 
varchar(
30
NOT 
NULL,
4   `tel` 
varchar(
30
NOT 
NULL,
5   `operate_date` date 
NOT 
NULL,
6   
PRIMARY 
KEY (`id`) )

 PHP脚本:

  1 operate 
list:
  2 <a href="curd.php?a=c">add</a>
  3 /
  4 <a href="curd.php?a=l">
list</a>
  5 <br />
  6 <br />
  7 <?php
  8 
$action = "l";
  9 
 10 
if (
isset(
$_REQUEST["a"])) {
 11     
$action = 
$_REQUEST["a"];
 12 }
 13 
 14 
if (
$action == "c") {                
//
add form
 15 
    showCreateForm();
 16 } 
elseif (
$action == "u") {            
//
update from
 17 
    showUpdateForm();
 18 } 
elseif (
$action == "l") {            
//
show list
 19 
    showList();
 20 } 
elseif (
$action == "save") {        
//
save
 21 
    
$name = 
$_REQUEST["name"];
 22     
$tel = 
$_REQUEST["tel"];
 23     
$operate_date = 
$_REQUEST["operate_date"];
 24     
 25     
$sql = "insert into task(name,tel,operate_date) values('
$name','
$tel','
$operate_date') ";
 26     executeSql(
$sql);
 27     
echo "save success !<br/>";
 28     showList();
 29 } 
elseif (
$action == "update") {    
//
update
 30 
    
$id = 
$_REQUEST["id"];
 31     
$name = 
$_REQUEST["name"];
 32     
$tel = 
$_REQUEST["tel"];
 33     
$operate_date = 
$_REQUEST["operate_date"];
 34     
 35     
$sql = "update task set name='
$name',tel='
$tel',operate_date='
$operate_date' where id='
$id' ";
 36     executeSql(
$sql);
 37     
echo "update success !<br/>";
 38     showList();
 39 } 
elseif (
$action == "d") {            
//
delete
 40 
    
$id = 
$_REQUEST["id"];
 41     
$sql = "delete from task where id='
$id'";
 42     executeSql(
$sql);
 43     
echo "delete success !<br/>";
 44     showList();
 45 }
 46 
 47 
function executeSql(
$sql){
 48     
$db = getMysqlConn();
 49     
$db->query(
$sql);
 50     
$db->close();
 51 }
 52 
 53 
function getList(
$sql) {
 54     
$db = getMysqlConn();
 55     
$result = 
$db->query(
$sql);
 56         
 57     
$num_results = 
$result->num_rows;
 58 
 59     
$data = 
array();
 60     
for (
$i = 0; 
$i < 
$num_results ; 
$i++) {
 61         
$row = (
array)
$result->fetch_assoc();
 62         
array_push(
$data
$row);
 63     }
 64     
 65     
$result->free();
 66     
$db->close();
 67     
return 
$data;
 68 }
 69 
 70 
function getMysqlConn(){
 71     @
$db = 
new mysqli("localhost","root","","galaxia_platform");
 72     
if (
mysqli_connect_errno()) {
 73         
echo "Error: connect mysql failed";
 74         
exit;
 75     }
 76     
return 
$db;
 77 }
 78 
 79 
function showCreateForm(){
 80     ?>
 81 <form action="curd.php?a=save" method="post">
 82     name:<input type="text" name="name"><br />
 83     tel:<input type="text" name="tel"><br />
 84     operate_date:<input type="text" name="operate_date"><br />
 85     <input type="submit" name="submit">
 86 </form>
 87     <?
 88 }
 89 
 90 
function showUpdateForm(){
 91     
$id = 
$_REQUEST ["id"];
 92     ?>
 93 <form action="curd.php?a=update" method="post">
 94     <? 
 95     
$sql = "select * from task where id = '
$id' ";
 96     
$data = getList(
$sql);
 97     
foreach(
$data 
as 
$task) {
 98     ?>
 99     <input type="hidden" name="id" value="<? echo 
$task["id"]?>"><br />
100     name:<input type="text" name="name" value="<? echo 
$task["name"]?>"><br />
101     tel:<input type="text" name="tel" value ="<? echo 
$task["tel"]?>"><br />
102     operate_date:<input type="text" name="operate_date" value="<? echo 
$task["operate_date"]?>"><br />
103     <input type="submit" name="submit">
104 </form>
105     <?
106     }
107 }
108 
109 
function showList(){
110     ?>
111 <table border="1">
112     <tr>
113         <th>name</th>
114         <th>tel</th>
115         <th>operate_date</th>
116         <th>operate</th>
117     </tr>
118     <? 
119     
$sql = "select * from task";
120     
$data = getList(
$sql);
121     
foreach(
$data 
as 
$task) {
122     ?>
123     <tr>
124         <td><? 
echo 
$task["name"]?></td>
125         <td><? 
echo 
$task["tel"]?></td>
126         <td><? 
echo 
$task["operate_date"]?></td>
127         <td><a href="curd.php?a=d&id=<? echo 
$task["id"]?>">del</a>/<a href="curd.php?a=u&id=<? echo 
$task["id"]?>">edit</a></td>
128     </tr>
129     <?
130     }
131     ?>
132 </table>
133 <?
134 }

135 ?> 

 

 

 

转载于:https://www.cnblogs.com/yimu/archive/2012/10/31/2747915.html

你可能感兴趣的文章
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>