偶的第一个PHP成品--留言板

来源:互联网 发布:ubuntu idea 添加字体 编辑:程序博客网 时间:2024/06/11 09:30

周末花了两个下午的时间终于把“留言版”敲完同时调试成功了。嘻嘻,坦白下一边敲代码还有一边开小差聊QQ~~

首先应该感谢 这位博主:http://blog.csdn.net/topfzy/archive/2007/06/09/1646000.aspx 把留言板的范例贴出来。

第二位感谢的就是偶同事了,帮我解决了好几个问题。下面偶把程序贴出来哩 虽然98%是抄上面那位大侠的,留作纪念!

 

包括:

存储的数据库:gbook

input.html为初始页
insert.php为把输入内容送入数据库

show.php显示留言板内容
change.php修改留言板内容的界面
change_ok.php修改留言板内容并送入数据库 

del.php删除留言板内容

 

创建数据库:

CREATE DATABASE GBOOK;

CREATE TABLE `gbook` (
`id` INT( 255 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 10 ) NOT NULL ,
`sex` TINYINT( 1 ) UNSIGNED NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`info` TEXT NOT NULL ,
`ip` VARCHAR( 15 ) NOT NULL ,
`time_at` DATETIME NOT NULL
)

input.html:

<!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/html; charset=utf-8" />
<title>留言版</title>
</head>

<body>
 <form name="form1" method="post" action="insert.php">
      <p>你的名字:<input type="text" name="name" size="20"/></p>
      <p>你的性别:<input type="radio" name="sex"  checked="checked"/>男生
        <input type="radio" name="sex"/>女生</p>
        <p>你的email:<input type="text" name="email" size="20" />
        </p>
        <p>你的留言内容:<textarea row="9" name="info" cols="35"></textarea></p>
        <p><input type="submit" value="提交" name="B1" />
         <input type="reset" value="重设" name="B2" />
        </p>
     </form>
</body>
</html>

insert.php:

<?php
 header("Content-Type:text/html; charset=utf-8");
 $mysql_server_name="localhost";
 $mysql_username="root";
 $mysql_password="123456";
 $mysql_database="gbook";
 $name=$_POST[name];
 $sex=$_POST[sex];
 $email=$_POST[email];
 $info=$_POST[info];
 $ip=getenv('REMOTE_ADDR');

 $conn=mysql_connect("localhost","root","123456");

 mysql_query('SET NAMES utf8');
 mysql_select_db("gbook");
 $sql="insert into `gbook`(id,name,sex,email,info,ip,time_at)values(null,'$name','$sex','$email','$info','$ip',NOW())";
 $result=mysql_query($sql,$conn);
 $id=mysql_insert_id();
 mysql_close($conn);
?>
<p>留言成功</p>
<p><a href="show.php">去留言页</a></p>

 

 

show.php:

<!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/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 <body>
<?php
 $mysql_server_name="localhost";
 $mysql_username="root";
 $mysql_password="123456";
 $mysql_database="gbook";
 $sql="select*from gbook order by id DESC";
 $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
 mysql_select_db($mysql_database,$conn);
 $result=mysql_query($sql);
 while($row=mysql_fetch_row($result))
 {
  if($row[2]==1)
  {$gender='男';}
  if($row[2]==0)
  $gender='女';
?>

<table>
 <tr>
     <td><p><?=$row[6]?><?=$row[5]?></p>
        <p><?=$row[1]?>(<?=$gender?>)<?=$row[3]?></p></td>
     </tr>
     <tr>
      <td><?=nl2br($row[4])?><p><a href="change.php?id=<?=$row[0]?>">[修改]</a><a href="del.php?id=<?=$row[0]?>">[删除]</a></p></td>
     </tr>
</table>
<hr/>
<?   
 }
 mysql_free_result($result);
?>
</body>
</html>

 

 

change.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
 $id = $_GET['id'];

 $mysql_server_name="localhost";
 $mysql_username="root";
 $mysql_password="123456";
 $mysql_database="gbook";
 
 $sql="select name,sex,email,info from gbook where id='$id'";
 $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
 mysql_select_db($mysql_database,$conn);
 $result=mysql_query($sql);
 $row=mysql_fetch_row($result);
 
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>留言版</title>
</head>

<body>
 <form name="form1" method="post" action="change_ok.php?id=<?=$id?>">
    <p>你的名字:<input type="text" name="name" size="20" value="<?=$row[0]?>"/></p>
<?
 if($row[1]==1)
 echo'<p> 你的性别:<input type="radio" value="1" name="sex" checked="checked">男 <input type="radio" value="0" name="sex">女 </p>';
 else
 echo'<p>你的性别:<input type="radio" value="0" name="sex" checked="checked">女 <input type="radio" value="1" name="sex">男</p>';
?>
<p>你的E-mail:<input type="text" name="email" size="20" value="<?=$row[2]?>"/></p>
<p>你的留言内容:<textarea name="info" rows="9" cols="35"><?=$row[3]?></textarea></p>
<p><input type="submit" value="提交"/><input type="reset" value="重置"/></p>
</form>
</body>
</html>

 

 

change_ok.php:

 

 

 

<!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/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?
 $id = $_GET['id'];

 $mysql_server_name="localhost";
 $mysql_username="root";
 $mysql_password="123456";
 $mysql_database="gbook";
 $name=$_POST[name];
 $sex=$_POST[sex];
 $email=$_POST[email];
 $info=$_POST[info];
 $sql="UPDATE `gbook` set `name`='$name', `sex`='$sex',`email`='$email',`info`='$info' where `id`='$id'";
 $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
 mysql_select_db($mysql_database,$conn);
 $result=mysql_query($sql);
 mysql_close($conn);
?>
<p><a href="show.php">返回</a></p>
</body>
</html>

 

 

del.php:

<!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/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?
 $id = $_GET['id'];

 $mysql_server_name="localhost";
 $mysql_username="root";
 $mysql_password="123456";
 $mysql_database="gbook";
 
 $sql="delete from gbook where id='$id'";
 $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
 mysql_select_db($mysql_database,$conn);
 $result=mysql_query($sql);
 mysql_close($conn);
?>
<p><a href="show.php">[返回]</a></p>
</body>
</html>

 

程序参考:http://blog.csdn.net/topfzy/archive/2007/06/09/1646000.aspx