Home
Backend Development
PHP Tutorial
CRUD (add, delete, modify, check) operations for php message board development_PHP tutorial



CRUD (add, delete, modify, check) operations for php message board development_PHP tutorial
Jul 21, 2016 pm 03:18 PM
crud
php
delete
increase
right
develop
operate
change
check
Add to
boundary
message board
of
structure
illustrate
page
project
Project structure:
Add page: Explanation: Only pay attention to the operation here, and there is no time for the artist of the interface. I hope everyone understands ...
List page:
Modify page:
SQL required in the project:
Copy code The code is as follows:
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;
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
Copy code The code is as follows:
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("Database link error");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
add.php $conn = @ mysql_connect("localhost", "root", "") or die("Database link error");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
Copy code The code is as follows:
<?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);
//Page jump, implemented by javascript
$url = "list.php";
echo "<script language='javascript' type='text /javascript'>";
echo "window.location.href='$url'";
echo "</script>";
}
?>
< ;script type="text/javascript">
function checkPost(){
if(addForm.user.value==""){
alert("Please enter username") ;
addForm.user.focus();
return false;
}
if(addForm.title.value.length<5){
alert("The title cannot be less than 5 characters");
addForm.title.focus();
return false;
}
}
</script>
<FORM name="addForm" METHOD= "POST" ACTION="add.php" onsubmit="return checkPost();">
User: <INPUT TYPE="text" NAME="user" /><br />
Title: <INPUT TYPE="text" NAME="title" /><br />
Content: <TEXTAREA NAME="content" ROWS="8" COLS="30"> </TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="add" /></FORM>
list .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);
//Page jump, implemented by javascript
$url = "list.php";
echo "<script language='javascript' type='text /javascript'>";
echo "window.location.href='$url'";
echo "</script>";
}
?>
< ;script type="text/javascript">
function checkPost(){
if(addForm.user.value==""){
alert("Please enter username") ;
addForm.user.focus();
return false;
}
if(addForm.title.value.length<5){
alert("The title cannot be less than 5 characters");
addForm.title.focus();
return false;
}
}
</script>
<FORM name="addForm" METHOD= "POST" ACTION="add.php" onsubmit="return checkPost();">
User: <INPUT TYPE="text" NAME="user" /><br />
Title: <INPUT TYPE="text" NAME="title" /><br />
Content: <TEXTAREA NAME="content" ROWS="8" COLS="30"> </TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="add" /></FORM>
Copy code The code is as follows:
<?php
include 'conn.php';
?>
<?php
echo "<div align='center'><a href='add.php'>继续添加</a></div>";
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql="select * from message order by id";
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td>标题:<font color="red"><?=$row[title]?></font> 用户:<font color="red"><?=$row[user] ?></font><div align="right"><a href="preEdit.php?id=<?=$row[id]?>">编辑</a> | <a href="delete.php?id=<?=$row[id]?>">删除</a></div></td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?=$row[content]?></td>
</tr>
<tr bgColor="#ffffff">
<td><div align="right">发表日期:<?=$row[lastdate]?></div></td>
</tr>
<?php }?>
</table>
delete.php
<?php
include 'conn.php';
$id = $_GET['id'];
$query="delete from message where id=".$id;
mysql_query($query);
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?>
preEdit.php
<?php
include 'conn.php';
$id=$_GET[id];
$query="SELECT * FROM message WHERE id =".$id;
$result=mysql_query($query);
while ($rs=mysql_fetch_array($result)){
?>
<FORM METHOD="POST" ACTION="postEdit.php">
<input type="hidden" name="id" value="<?=$rs[id]?>">
用户:<INPUT TYPE="text" NAME="user" value="<?=$rs[user]?>"/><br />
标题:<INPUT TYPE="text" NAME="title" value="<?=$rs[title]?>"/><br />
内容:<TEXTAREA NAME="content" ROWS="8" COLS="30"><?=$rs[content]?></TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="edit"/>
</FORM>
<?php }?>
postEdit.php
<?php
include 'conn.php';
$query="update message set user='$_POST[user]',title='$_POST[title]',content='$_POST[content]' where id='$_POST[id]'";
mysql_query($query);
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?>
<?php
include 'conn.php';
?>
<?php
echo "<div align='center'><a href='add.php'>继续添加</a></div>";
?>
<table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?php
$sql="select * from message order by id";
$query=mysql_query($sql);
while ($row=mysql_fetch_array($query)){
?>
<tr bgcolor="#eff3ff">
<td>标题:<font color="red"><?=$row[title]?></font> 用户:<font color="red"><?=$row[user] ?></font><div align="right"><a href="preEdit.php?id=<?=$row[id]?>">编辑</a> | <a href="delete.php?id=<?=$row[id]?>">删除</a></div></td>
</tr>
<tr bgColor="#ffffff">
<td>内容:<?=$row[content]?></td>
</tr>
<tr bgColor="#ffffff">
<td><div align="right">发表日期:<?=$row[lastdate]?></div></td>
</tr>
<?php }?>
</table>
delete.php
复制代码 代码如下:
<?php
include 'conn.php';
$id = $_GET['id'];
$query="delete from message where id=".$id;
mysql_query($query);
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?>
preEdit.php
复制代码 代码如下:
<?php
include 'conn.php';
$id=$_GET[id];
$query="SELECT * FROM message WHERE id =".$id;
$result=mysql_query($query);
while ($rs=mysql_fetch_array($result)){
?>
<FORM METHOD="POST" ACTION="postEdit.php">
<input type="hidden" name="id" value="<?=$rs[id]?>">
用户:<INPUT TYPE="text" NAME="user" value="<?=$rs[user]?>"/><br />
标题:<INPUT TYPE="text" NAME="title" value="<?=$rs[title]?>"/><br />
内容:<TEXTAREA NAME="content" ROWS="8" COLS="30"><?=$rs[content]?></TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="edit"/>
</FORM>
<?php }?>
postEdit.php
复制代码 代码如下:
<?php
include 'conn.php';
$query="update message set user='$_POST[user]',title='$_POST[title]',content='$_POST[content]' where id='$_POST[id]'";
mysql_query($query);
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
