最简单的PHP+MYSQL留言板

来源:互联网 发布:怎么做好淘宝代理 编辑:程序博客网 时间:2024/05/16 04:59

最简单的PHP留言板,那么久一直想做出来,总拖着,现在终于抽时间搞定了。。。

因为比较简单,不再做介绍,看注释!

config.php

<?php 
/*
  数据库文件
  --
-- 数据库: `fy`
--
-- 表的结构 `fy_leave`
--


CREATE TABLE `fy_leave` (
  `id` int(12) NOT NULL auto_increment,
  `title` varchar(64) character set utf8 collate utf8_unicode_ci NOT NULL,
  `message` text character set utf8 collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;  
 */


 $conn=mysql_connect("localhost","root",""); 
 if(!$conn)
 {
   die('error'.mysql_error());
 }
  mysql_select_db("fy",$conn); 
?>

index.php

<?php
 require_once("config.php");
?>
<center><h2>枫杨---留言板</h2></center>




<?php // 循环输出数据库内容
 $query='select * from fy_leave';
 $result=mysql_query($query);
 while($row=mysql_fetch_array($result))
 {
  echo "<ul><li> 名称:".$row['title']."&nbsp;&nbsp;留言:".$row['message'];
  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="update.php?id='.$row['id'].'">修改</a>||<a href="delete.php?id='.$row['id'].'">删除</a></li></ul><hr>';
 }
?>
<hr><br><br>
<?php //添加新数据的表单?>
<center><form action="add.php" method="post">
<b>名称:<input style="border:1px solid #ccc;border-radius:5px;"name="title" value=""/></b><br>
<b>内容:<br><textarea style="border:1px solid #ccc;border-radius:5px;" name="message" cols='100' rows='10'/></textarea><br>
<input type="submit" name="submit" value="提交"/>&nbsp;&nbsp;<input type="button" name="reset" value="重置"/>
</form>
</center>

add.php

<?php 
//添加一项留言
include("config.php");
$title=$_POST['title'];
$message=$_POST['message'];
$query="INSERT INTO `fy`.`fy_leave` (`id`, `title` ,`message`) value('','$title','$message')";
 mysql_query($query);
 echo "<script>location.href='index.php'</script>";
?>


delete.php

<?php 
//删除一项留言
 require_once("config.php");
 $id=$_GET['id'];//获取ID,根据ID 删除对应项
 $query="delete from fy_leave where id='$id'";
 mysql_query($query);
 echo "<script>location.href='index.php'</script>"; //返还到首页
?>

update.php

<?php
 require("config.php");
 $id=$_GET['id']; //获取ID,根据ID更新
 //更具修改内容更新文件
 $query="select * from fy_leave where id='$id'";
 $result=mysql_query($query);
 $row=mysql_fetch_array($result);
?>
<h1>修改留言</h1>
<form action="updatepost.php?id=<?php echo $row['id'];?>" method="post">
<b>名称:<input style="border:1px solid #ccc;border-radius:5px;"name="title" value="<?php echo $row['title'];?>"/></b><br>
<b>内容:<br><input style="border:1px solid #ccc;border-radius:5px;" name="message" cols='100' rows='10' value="<?php echo $row['message'];?>"><br>
<input type="submit" name="submit" value="修改"/>&nbsp;&nbsp;<input type="button" name="reset" value="重置"/>
</form>


updatepost.php

<?php
 require 'config.php';
 $id=$_GET['id'];
 $title=$_POST['title'];
 $message=$_POST['message'];
 $query="update fy_leave SET title='$title',message='$message' where id='$id'";
 mysql_query($query);
 echo "<script>location.href='index.php'</script>";


?>

原创粉丝点击