现在的位置: 首页 > 综合 > 正文

php学习笔记之留言板制作

2013年09月17日 ⁄ 综合 ⁄ 共 3428字 ⁄ 字号 评论关闭

寒假时学习PHP语言,也做了一个很简单的留言板,但是对知识了解的还是不是很深入,下面进行一下总结和梳理。

一、目的:书写简单的php留言板:
二、准备阶段:
1、必备环境:apache+php+mysql(数据库)
2、必备知识:简单php语言,简单mysql语句
3、用到的数据库语句:
增:Insert into表(字段1,字段2...) values(值1,值2..) 
Insert into 表 values(值1,值2..)
Insert into 表  set 字段1=值1,字段2=值2
查:
Select 字段,字段,…… from 表 
Select   *  或  函数(字段) from 表//*表示所有字段
Select 字段,字段,…… from   表  where  字段  in  (值,值)
Select 字段,字段,…… from   表  where  字段  not  in (值,值)
Select 字段,字段,…… from   表  group by 字段 
Select 字段,字段,…… from   表  order by 字段 [asc / desc ]//asc表示正序,desc表示倒序
Select 字段,字段,…… from   表    limit    起始位   ,条数
改:
Update 表 set 字段=值 , 字段=值  [where] [group] [order] [limit] 
删:delete from 表 [where] [group] [order] [limit] 
其他:
mysql_query("sql语句或者命令")//返回资源类型
mysql_fetch_array()//返回下标和键数组
mysql_fetch_row()//返回下标数组;
mysql_fetch_object//返回对象形式调用;
mysql_num_rows(数据资源)//获得数据源中的数据条数,返回int型;

三、具体步骤:
1、连接数据库文件(conn.php):
一共分为三个步骤:
(1)、连接数据库
(2)、选择数据库
(3)、设置编码(gbk,gb2312...)
代码如下:
<?php

@mysql_conntct("localhost:3306","root","123456")or die("mysql连接失败");//@表示屏蔽报错,or die()语句,判断是否错误。

@mysql_select_db("bbs")or die("db连接失败");
mysql_set_charset("gbk");//或者是mysql_query("set_name 'gdk'");

?>

2、添加的文件(add.php)

下面做一个添加页面
代码如下:
<html>
<title>留言板<title>
<?php
include("conn.php");/引入数据库
if(!empty($_POST['sub']))/*这里的$_POST 变量用于收集来自 method="post" 的表单中的值。*/
{
$title=$_POST['title'];
$con=$_POST['con'];
$huifu=$_POST['huifu'];
$sql="insert into `new` (`id`,`title`,`dates`,`contents`,`huifu`)value (null,'title',now(),'$con','')";//`contents`,contents两边的符号不是单引号,而是小撇“ ` ”
if(mysql_query($sql))
{
echo "<script>alert('发表成功');location.href='index.php'</script>";
else
echo "<script>alert('错误');location.href='index.php'</script>";

}

}
?>
//下面写的是html代码

<form action="add.php" method="post">
标题<input type="text" name="title"><br/>
内容<textarea rows="5" cols="50" name="con"></textarea><br/>
<input type="submit" name="sub" value="发表">
</form>
</html>

3、编写主页面(index.php):

<html>

<title>留言板</title>
<a href='add.php'>留言</a><hr><hr>
<?php
include("conn.php");//引入数据库;
$sql =“select * from `news` order by id desc limit 10”;//desc倒序 limit 10只显示10条数据。
$query =mysql_query($sql);

while($rs = mysql_fetch_array ($query) )//循环语句

{

?>
/*下面是html代码*/

<h3>用户:<?php echo $rs['name'] ?></h3>
<h4>标题:<?php echo $rs['title'] ?> </h4>
<li>时间:<?php echo $rs['dates'] ?> </li>
<p><?php echo $rs['contents'] ?></p>
回复:<p><?php echo $rs ['huifu']?></p>
<a href="del.php?del=<?php echo $rs['id'] ?>">|删除</a>|<a href="huifu.php?id=?<?php echo $rs['id'] ?>">回复</a>

<hr>

<?php
}

?>
</html>

4.编写删除页面(del.php)

<?php
   include("conn.php");//引入连接数据库
if ($_GET['del']) {
$d=$_GET['del'];
$sql ="delete from `news` where `id`='$d'";

mysql_query($sql);
echo "<script>alert('删除成功');location.href='index.php'</script>";
}

?>
5.编写回复页面(huifu.php)
说是回复页面,并不是严格意义上的回复。实际上就是一个修改数据的页面

<html>
<title>留言板</title>
<?php
   include("conn.php");//引入连接数据库
   
   if(($_GET['id'])){
   $hid=$_GET['id'];
   $sql="select * from news where `id`=$hid";
   $query = mysql_query ($sql);
   $rs = mysql_fetch_array ($query);
 
 if ($_POST['sub'])
{

$huifu=$_POST['hui'];
$hidd=$_POST['hid'];
$sql="update `news` set `huifu`='$huifu' where id='$hid'";

if(mysql_query($sql))
{
echo "<script>alert('回复成功');location.href='index.php'</script>";
}else 
{
echo "<script>alert('错误');location.href='index.php'</script>";
}

?>
/*html代码*/

<form action="huifu.php?id=<?php echo $rs['id'];?>" method ="post">
<input type="hidden" name="hid" value="<?php echo $rs['id']?>">
回复<br/><textarea rows="5" cols="50" name="hui" value="<?php echo $rs['huifu'] ?>"></textarea><br/>
<input type="submit" name="sub" value="发表">
</form>

</html>

现在整个留言板就制作成功了。
注:在写代码的时候一定要仔细,因为mysql语句有很多的标点符号,所以很容易出现bug,而且这种bug也比较难找。友情提示:不要复制上面的内容,因为可能会有很多bug。
debug:1、一步一步的显示,哪里输出不了,就echo变量。
      2、注意id的A_I开关要打开

抱歉!评论已关闭.