php开发留言板的CRUD(增,删,改,查)操作_PHP教程
程序员文章站
2024-04-03 17:04:58
...
项目结构:
create database form;
use form;
CREATE TABLE `message` (
`id` tinyint(1) NOT NULL auto_increment,
`user` varchar(25) NOT NULL,
`title` varchar(50) NOT NULL,
`content` tinytext NOT NULL,
`lastdate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;
conn.php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
add.php
include 'conn.php';
if($_POST['submit']){
$sql="INSERT INTO message(id,user,title,content,lastdate) VALUES (NULL, '$_POST[user]', '$_POST[title]', '$_POST[content]', now())";
mysql_query($sql);
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
}
?>
list.php
include 'conn.php';
?>
echo "";
?>
delete.php
include 'conn.php';
$id = $_GET['id'];
$query="delete from message where id=".$id;
mysql_query($query);
?>
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
?>
preEdit.php
include 'conn.php';
$id=$_GET[id];
$query="SELECT * FROM message WHERE id =".$id;
$result=mysql_query($query);
while ($rs=mysql_fetch_array($result)){
?>
postEdit.php
include 'conn.php';
$query="update message set user='$_POST[user]',title='$_POST[title]',content='$_POST[content]' where id='$_POST[id]'";
mysql_query($query);
?>
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
?>
添加页面: 说明:这里只注重操作,对界面的美工没有下工夫,希望大家理解......
列表页面:
修改页面:
项目中所需的sql:
复制代码 代码如下:
create database form;
use form;
CREATE TABLE `message` (
`id` tinyint(1) NOT NULL auto_increment,
`user` varchar(25) NOT NULL,
`title` varchar(50) NOT NULL,
`content` tinytext NOT NULL,
`lastdate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;
conn.php
复制代码 代码如下:
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
add.php
复制代码 代码如下:
include 'conn.php';
if($_POST['submit']){
$sql="INSERT INTO message(id,user,title,content,lastdate) VALUES (NULL, '$_POST[user]', '$_POST[title]', '$_POST[content]', now())";
mysql_query($sql);
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
}
?>
list.php
复制代码 代码如下:
include 'conn.php';
?>
echo "";
?>
标题:=$row[title]?> 用户:=$row[user] ?> |
内容:=$row[content]?> |
发表日期:=$row[lastdate]?> |
delete.php
复制代码 代码如下:
include 'conn.php';
$id = $_GET['id'];
$query="delete from message where id=".$id;
mysql_query($query);
?>
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
?>
preEdit.php
复制代码 代码如下:
include 'conn.php';
$id=$_GET[id];
$query="SELECT * FROM message WHERE id =".$id;
$result=mysql_query($query);
while ($rs=mysql_fetch_array($result)){
?>
postEdit.php
复制代码 代码如下:
include 'conn.php';
$query="update message set user='$_POST[user]',title='$_POST[title]',content='$_POST[content]' where id='$_POST[id]'";
mysql_query($query);
?>
//页面跳转,实现方式为javascript
$url = "list.php";
echo "";
?>
推荐阅读