Rumah > php教程 > PHP源码 > php开发留言板的CRUD(增,删,改,查)操作_php实例

php开发留言板的CRUD(增,删,改,查)操作_php实例

PHP中文网
Lepaskan: 2016-05-25 17:06:05
asal
1166 orang telah melayarinya

php学习中,以开放一个留言本作为php的入门资料还是不错的。

项目结构:

添加页面:                                说明:这里只注重操作,对界面的美工没有下工夫,希望大家理解......

列表页面:

修改页面:

项目中所需的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 ;
Salin selepas log masuk

conn.php

<?php 
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); 
mysql_select_db("form", $conn); 
mysql_query("set names &#39;gbk&#39;"); 
?>
Salin selepas log masuk

add.php

<?php 
include &#39;conn.php&#39;; 
if($_POST[&#39;submit&#39;]){ 
$sql="INSERT INTO message(id,user,title,content,lastdate) VALUES (NULL, &#39;$_POST[user]&#39;, &#39;$_POST[title]&#39;, &#39;$_POST[content]&#39;, now())"; 
mysql_query($sql); 
//页面跳转,实现方式为javascript 
$url = "list.php"; 
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>"; 
echo "window.location.href=&#39;$url&#39;"; 
echo "</script>"; 
} 
?> 
<script type="text/javascript"> 
function checkPost(){ 
if(addForm.user.value==""){ 
alert("请输入用户名"); 
addForm.user.focus(); 
return false; 
} 
if(addForm.title.value.length<5){ 
alert("标题不能少于5个字符"); 
addForm.title.focus(); 
return false; 
} 
} 
</script> 
<FORM name="addForm" METHOD="POST" ACTION="add.php" onsubmit="return checkPost();"> 
用户:<INPUT TYPE="text" NAME="user" /><br /> 
标题:<INPUT TYPE="text" NAME="title" /><br /> 
内容:<TEXTAREA NAME="content" ROWS="8" COLS="30"></TEXTAREA><br /> 
<INPUT TYPE="submit" name="submit" value="add" /></FORM>
Salin selepas log masuk

list.php

<?php 
include &#39;conn.php&#39;; 
?> 
<?php 
echo "<p align=&#39;center&#39;><a href=&#39;add.php&#39;>继续添加</a></p>"; 
?> 
<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><p align="right"><a href="preEdit.php?id=<?=$row[id]?>">编辑</a>  |  <a href="delete.php?id=<?=$row[id]?>">删除</a></p></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td>内容:<?=$row[content]?></td> 
</tr> 
<tr bgColor="#ffffff"> 
<td><p align="right">发表日期:<?=$row[lastdate]?></p></td> 
</tr> 
<?php }?> 
</table>
Salin selepas log masuk

delete.php

<?php 
include &#39;conn.php&#39;; 
$id = $_GET[&#39;id&#39;]; 
$query="delete from message where id=".$id; 
mysql_query($query); 
?> 
<?php 
//页面跳转,实现方式为javascript 
$url = "list.php"; 
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>"; 
echo "window.location.href=&#39;$url&#39;"; 
echo "</script>"; 
?>
Salin selepas log masuk

preEdit.php

<?php 
include &#39;conn.php&#39;; 
$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 }?>
Salin selepas log masuk

postEdit.php

<?php 
include &#39;conn.php&#39;; 
$query="update message set user=&#39;$_POST[user]&#39;,title=&#39;$_POST[title]&#39;,content=&#39;$_POST[content]&#39; where id=&#39;$_POST[id]&#39;"; 
mysql_query($query); 
?> 
<?php 
//页面跳转,实现方式为javascript 
$url = "list.php"; 
echo "<script language=&#39;javascript&#39; type=&#39;text/javascript&#39;>"; 
echo "window.location.href=&#39;$url&#39;"; 
echo "</script>"; 
?>
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan