php留言本

最近学PHP 参考手册写了这么个留言本.没有什么难点.就是些php操作mysql数据库函数的应用.加个自己的思路
 
注意编码问题
 
代码如下:
 
//提交表单和处理显示的页面

<!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>PHP留言本</title>
<style>
body,h1,form{margin:0;padding:0;}
input,textarea{ vertical-align:middle;}
.t{ font-size:18px;color:#fff; font-family:"黑体"}
.page{width:950px; margin:0 auto; background:#0C3;padding:20px; text-align:center;}
.notebook{ font-size:14px;color:#FFF;}
.username input,.contact input{width:120px;height:25px;border:#fff solid 2px;margin-top:10px; color:#090;}
.message textarea{ width:400px;height:300px; overflow:hidden; border:#fff 5px solid;margin-top:10px; color:#090;}
.btn{margin-top:10px;}
.main{ text-align:center; margin-top:50px;}
.back,.back td{ border-collapse:collapse;border:#090 1px solid; background:#fff; color:#090;}
</style>
</head>

<body>
<div class="page">
<h1 class="t">PHP留言本</h1>
<form action="noteform.php" method="post" name="notebook" class="notebook">
<div class="username">您的姓名:<input type="text" name="username"    /></div>
<div class="contact">联系方式:<input type="text" name="contact" /></div>
<div class="message">信息:<textarea name="message"></textarea></div>
<input type="submit" class="btn" value="留言" />
</form>

<div class="main">
<table border="0" cellspacing="0" cellpadding="0" class="back">
<?php

  $link = mysql_connect('localhost','root','改成自己的数据库密码');
  if (!$link) {
    die('could not connect :' .mysql_error());    
  }
  mysql_select_db('notebook',$link); //选择数据库
  mysql_query("set names utf-8"); //解决中文乱码问题
  $selectdb = "select * from info";
  $rs = mysql_query($selectdb);    
  while ($row = mysql_fetch_object($rs)){ //返回对象
    echo "<tr>";
    echo "<td>姓名:".$row->username."</td>";
    echo "<td>联系方式:".$row->contact."</td>";
    echo "<td>留言时间:".$row->dt."</td>";
    echo "<td>留言时间:".$row->message."</td>";
    echo "</tr>";
  }
  mysql_free_result($rs);

?>
</table>


<table width="200" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td></td>
        <td> </td>
    </tr>
    <tr>
        <td> </td>
        <td> </td>
    </tr>
</table>


</div>

</div>
</body>
</html>
 
第二个页面:
 
<?php

$username = $_POST["username"];
$contact = $_POST["contact"];
$message = $_POST["message"];
$link = mysql_connect('localhost','root','#####');
if (!$link) {
  die('could not connect :' .mysql_error());    
}
mysql_select_db('notebook',$link); //选择数据库
mysql_query("set names utf-8"); //解决中文乱码问题
/*将表单信息插入数据库*/
mysql_query("insert into info (username,contact,dt,message) values ('$username','$contact',now(),'$message')",$link);    
header("Location: http://localhost/notebook.php");

?>

你可能感兴趣的:(职场,休闲,php留言本)