php怎么在mysql创建留言板步骤

php怎么在mysql创建留言板步骤,第1张

工具:

Dreamweaver

php、mysql服务器

步骤/方法

首先是确定自己的留言板需求.例如:名字,邮件及留言内容.

一. 建立一个数据库guestbook。

CREATE TABLE IF NOT EXISTS `content` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(20) NOT NULL,

`email` varchar(50) NOT NULL,

`content` varchar(200) NOT NULL,

PRIMARY KEY (`id`))

ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3

二. 新建config.php

< ? php

$q = mysql_connect("服务器","数据库用户","数据库密码")

if(!$q)

{

die('Could not connect: ' . mysql_error())

}

mysql_query("set names utf8")//以utf8读取数据

mysql_select_db("guestbook",$q) //数据库

?>

三. 新建index.php

<?php

include("config.php") //引入数据库连接文件

$sql = "select * from content" //搜索数据表content

$resule = mysql_query($sql,$q)

?>

<html>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<body>

<table width="678" align="center">

<tr>

<td colspan="2"><h1>留言本</h1></td>

</tr>

<tr>

<td width="586"><a href="index.php">首页</a>| <a href="liuyan.php">留言</a></td>

</tr>

</table>

<p>

<?

while($row=mysql_fetch_array($resule))

{

?>

</p>

<table width="678" border="1" align="center" cellpadding="1" cellspacing="1">

<tr>

<td width="178">Name:<? echo $row[1] ?></td>

<td width="223">Email:<? echo $row[2] ?></td>

</tr>

<tr>

<td colspan="4"><? echo $row[3] ?></td>

</tr>

<tr>

</table>

<?

}

?>

</body>

</html>

四. 新建liuyan.php

< html>

<body>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<table width="678" align="center">

<tr>

<td colspan="2"><h1>留言本</h1></td>

</tr>

<tr>

<td width="586"><a href="index.php">首页</a>| <a href="liuyan.php">留言</a></td>

</tr>

</table>

<table align="center" width="678">

<tr>

<td>

<form name="form1" method="post" action="post.php">

<p>

Name:

<input name="name" type="text" id="name">

</p>

<p>Email:<input type="test" name="email" id="email"></p>

<p>

留言:

</p>

<p>

<textarea name="content" id="content" cols="45" rows="5"></textarea>

</p>

<p>

<input type="submit" name="button" id="button" value="提交">

<input type="reset" name="button2" id="button2" value="重置">

</p>

</form>

</td>

</tr>

</table>

</body>

</html>

五. 新建post.php

< ?php

header("content-Type: text/htmlcharset=utf-8")

include("config.php")

$name= $_POST['name']

$email= $_POST['email']

$patch = $_POST['content']

$content = str_replace("

","<br />",$patch)

$sql = "insert into content (name,email,content) values ('$name','$email','$content')"

mysql_query($sql)

echo "<script>alert('提交成功!返回首页。')location.href='index.php'</script>"

?>

这样已经成功的写出一个留言板了。

第二部分

此次将在上面版本上加多管理,回复等功能。

首先在sql中字节。

ALTER TABLE `content` ADD `reply` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `content`

一. 新建login.php

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

</head>

<body><table width="678" align="center">

<tr>

<td colspan="2"><h1>留言本</h1></td>

</tr>

<tr>

<td width="586"><a href="index.php">首页</a>| <a href="liuyan.php">留言</a></td>

</tr>

</table>

<table align="center" width="678">

<tr>

<td align="center">

<form name="form1" method="post" action="login2.php">

<label for="textfield"></label>

<p></p>

<p>帐号:

<input type="text" name="name" id="name">

</p>

<p>密码:

<input type="password" name="pw" id="pw">

</p>

<p>

<input type="submit" name="button" id="button" value="提交">

<input type="reset" name="button2" id="button2" value="重置">

</p>

</form></td>

</tr>

</table>

</body>

</html>

二.login2.php

<?

session_start()

header("content-Type: text/htmlcharset=utf-8")

$name = $_POST['name']

$pw = $_POST['pw']

if($name == "admin" &&$pw == "admin"){

$_SESSION["adminname"] = $name

echo "<script>alert('登录完成,返回首页!')location.href='index.php'</script>"

}else{

echo "<script>alert('错误!')location.href='login.php'</script>"

}

?>

三. 在原有的index.php上添加

< ?php

session_start()

include("config.php")

$sql = "select * from content"

$resule = mysql_query($sql,$q)

?>

< html>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<body>

<table width="678" align="center">

<tr>

<td colspan="2"><h1>留言本</h1></td>

</tr>

<tr>

<td width="586"><a href="index.php">首页</a>| <a href="liuyan.php">留言</a></td>

<td width="80">

// 新增管理员登录

< ?php

if (isset($_SESSION['adminname']) &&$_SESSION["adminname"] == "admin"){

echo "<a href='logout.php'>登出</a>"

}else{

echo "<a href='login.php'>管理员登录</a>"

}

?>

</td>

</tr>

</table>

<p>

<?

while($row=mysql_fetch_array($resule))

{

?>

</p>

<table width="678" border="1" align="center" cellpadding="1" cellspacing="1">

<tr>

<td width="178">Name:<? echo $row[1] ?></td>

<td width="223">Email:<? echo $row[2] ?></td>

<td width="100">

< ?php

if(isset($_SESSION['adminname']) &&$_SESSION["adminname"] == "admin"){

echo "<a href='huifu.php?id=" . $row[0] . "'>回复</a>"

echo " | " . "<a href='delete.php?id=" . $row[0] . "'>删除</a>"

} else {

echo ""

}

?>

</td>

</tr>

<tr>

<td colspan="4"><? echo $row[3] ?></td>

</tr>

<tr>

<td colspan="4"><?

if($row[4] == ""){

?>

<? echo "暂无回复。"?>

<? }else {echo "管理员回复:". $row[4]} ?></td>

</tr>

</table>

<?

}

?>

</body>

</html>

四. 新建huifu.php

< ?php

include("config.php")

$sql = "select * from content where id=".$_GET["id"]

$resule = mysql_query($sql,$q)

SetCookie("id",$_GET["id"])

session_start()

header("content-Type: text/htmlcharset=utf-8")

if(empty($_SESSION["adminname"])){

exit("<script language='javascript'>alert('您尚未登录后台,或登录已超时,请重新登录!')window.location.href='login.php'</script>")

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

</head>

<body>

<table width="678" align="center">

<tr>

<td colspan="2"><h1>留言本</h1></td>

</tr>

<tr>

<td width="586"><a href="index.php">首页</a>| <a href="liuyan.php">留言</a></td>

</tr>

</table>

<table align="center" width="678">

<tr>

<td>

<form name="reply" method="post" action="reply.php" >

<p>回复:

<?

while($row=mysql_fetch_array($resule))

{

echo $row[3]

}

?>

</p>

<p>

<textarea name="reply" id="reply" cols="45" rows="5"></textarea>

</p>

<p>

<input type="submit" name="button" id="button" value="回复" />

<input type="reset" name="button2" id="button2" value="重置" />

</p>

</form>

</td>

</tr>

</table>

</body>

</html>

五. reply.php 回复留言提交页面.

<?

include("config.php")

$id = $_COOKIE["id"]

$sql = "select * from content"

header("content-Type: text/htmlcharset=utf-8")

$patch = $_POST["reply"]

$reply = str_replace("

","<br />",$patch)

$resule = mysql_query("UPDATE `2`.`content` SET `reply` = '$reply' WHERE `content`.`id` ="."$id")

echo "<script>alert('回复成功!')location.href='index.php'</script>"

?>

这样就可以建立出一个简单的管理.管理帐号都是admin 因为只是判别输入的是不是admin 是的话就把值输入进session中.

登录页:login.php

<?php

include("conn.php")

$username=$_POST['name']

$password=$_POST['password']

$yanzheng=$_POST['yanzheng']

if(isset($_POST['submit']))

{

$sql=("select username,password from member where username='$username' and password='$password'") or die("sql语句执行失败")

//print_r($sql)

$ar=mysql_query($sql)

if($ar)

{

if($row=mysql_fetch_array($ar))

{

session_start()

if($_POST["yanzheng"])

{

if($yanzheng!=$_session[pic]||$yanzheng=="")

{

echo "验证码输入有误"

exit

}

if($yanzheng==$_session[pic])

{

header("location:index.php")

}

}

}

else

{

echo "用户名或密码错误"

}

}

}

?>

<form action="login.php" method="post">

<table border=1 align=center width=500 height=300 bgColor=#DFFFDF bordercolor=#fffbec>

<tr>

<td colspan=2 align=center>用户登录</td>

</tr>

<tr>

<td>用户姓名:</td>

<td><input type="text" name="name" id="name"/></td>

</tr>

<tr>

<td>用户密码:</td>

<td><input type="password" name="password" id="password"/></td>

</tr>

<tr>

<td>验证码:</td>

<td><input type="text" name="yanzheng" id="yanzheng"/>

<img src="yanzheng1.php" width="50" height="30"></img>

</td>

</tr>

<tr>

<td colspan=3 align=center>

<input type="submit" name="submit" value="登录"/>

<input type="reset" name="reset" value="重置"/>

<a href="register.php">注册</a>

</td>

</tr>

</table>

</form>

注册页:register.php

<?php

include("conn.php")

if(isset($_POST['submit'])&&$_POST['submit']) {

if($_POST['username']=='')

{

echo "用户名不能为空"

exit()

}

if($_POST['password']=='')

{

echo "密码不能为空"

exit()

}

if($_POST['realpass']!=$_POST['password'])

{

echo "两次密码输入不一致"

exit()

}

$sql="insert into member(username,real_name,password,email,headimg) values('$_POST[username]','$_POST[username]','$_POST[password]','$_POST[email]','')"

$ar=mysql_query($sql)

if($ar)

{

header("location:index.php")

}

else

{

echo mysql_error()

}

}

?>

<body>

<form action="register.php" method="post">

<table border=1 align=center width=500>

<tr>

<td height=40 bgColor=#DFFFDF colspan=2>会员注册          [<a href="login.php">返回登录页</a>]</td>

</tr>

<tr>

<td height=40 bgColor=#fffbec >会员ID</td>

<td><input type="text" name="username" id="username"/></td>

</tr>

<tr>

<td height=40 bgColor=#fffbec>密码</td>

<td><input type="password" name="password" id="password"/></td>

</tr>

<tr>

<td height=40 bgColor=#fffbec>确认密码</td>

<td>

<input type="password" name="realpass" id="realpass"/>

</td>

</tr>

<tr>

<td height=40 bgColor=#fffbec>EMAIL</td>

<td><input type="text" name="email" id="email"/>

</tr>

<tr>

<td height=40 bgColor=#fffbec></td>

<td><input type="submit" name="submit" value="注册"/><input type="reset" value="重置"></td>

</tr>

</table>

</form>

</body>

主页显示:index.php

<?php

include("conn.php")

function cutstr($str,$cutleng)

{

$str = $str//要截取的字符串

$cutleng = $cutleng//要截取的长度

$strleng = strlen($str)//字符串长度

if($cutleng>$strleng)return $str//字符串长度小于规定字数时,返回字符串本身

$notchinanum = 0//初始不是汉字的字符数

for($i=0$i<$cutleng$i++)

{

if(ord(substr($str,$i,1))<=128)

{

$notchinanum++

}

}

if(($cutleng%2==1)&&($notchinanum%2==0)) //如果要截取奇数个字符,所要截取长度范围内的字符必须含奇数个非汉字,否则截取的长度加一

{

$cutleng++

}

if(($cutleng%2==0)&&($notchinanum%2==1)) //如果要截取偶数个字符,所要截取长度范围内的字符必须含偶数个非汉字,否则截取的长度加一

{

$cutleng++

}

return substr($str,0,$cutleng)

}

?>

<html>

<head>

<script type="text/javascript">

function All(e, itemName)

{

var aa = document.getElementsByName(itemName)

for (var i=0i<aa.lengthi++)

aa[i].checked = e.checked//得到那个总控的复选框的选中状态

}

function Item(e, allName)

{

var all = document.getElementsByName(allName)[0]

if(!e.checked) all.checked = false

else

{

var aa = document.getElementsByName(e.name)

for (var i=0i<aa.lengthi++)

if(!aa[i].checked) return

all.checked = true

}

}

</script>

</head>

<?php

include("conn.php")

if(isset($_POST['del']))

{

$mm = $_POST["selected"]

$id =implode(",",$mm)

$sql = "delete from forums where id in(".$id.")"

//echo $sql

$result=mysql_query($sql)

echo $result?"删除成功":"删除失败"

}

?>

<table style="BORDER-BOTTOM-WIDTH: 1pxBORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=600 align=center border=1 bordercolor=#ddddff>

<tr align=middle>

<td height=40 bgColor=#DFFFDF colspan=3>论坛列表</td>

</tr>

<tr>

<td colspan=3><a href="login.php" style="float:right">[退出系统]</a><a href="add_forum.php" style="float:right">[添加论坛]</a></td>

<td></td>

</tr>

<tr align=middle>

<td height=40 bgColor=#DFFFDF width=80>状态</td>

<td height=40 bgColor=#DFFFDF>论坛</td>

<td height=40 bgColor=#DFFFDF>最后更新</td>

</tr>

<?php

$sql="select * from forums"

$result=mysql_query($sql)

$num=mysql_num_rows($result)

if($num>0)

{

while($row=mysql_fetch_array($result)){

?>

<tr align=middle>

<td bgColor=#fffbec><input type="checkbox" name="selected" value="1"/></td>

<td height=50 bgColor=#fffbec width=300>

<?php

echo "<div><a href=\"forums.php?F=".$row['ID']."\">".$row['forum_name']."</a></div>"

echo cutstr($row['forum_description'],24)//最多显示24个字节,12个字,多余部分用省略号代替

echo "……"

?>

</td>

<td height=50 bgColor=#fffbec><div><?php echo $row['last_post_time']."by".$row['last_post_author']?></div></td>

</tr>

<?php

}

}

else

{

echo "<tr bgColor=#fffbec><td colspan=3>对不起,论坛尚在创建中……</td></tr>"

}

?>

<tr>

<td colspan=3>    <input type="checkbox" name="selected" value="1" onclick="All(this,'selected')"/>全选/不全选</td>

</tr>

<tr>

<td><input type="button" name="del" id="del" value="删除选中项"/>

<?php

?>

</td>

</tr>

</table>

</html>

自己创建数据库就好。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7333694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存