創建數據庫:
create database stud;
use stud;
create table student(username char(20) not null,message char(200) not null);
PHP代碼:
文件index.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>留言板</title>
</head>
<body>
<SCRIPT LANGUAGE="javascript">
<!--
function openwin(){
window.open ('message.html', '添加留言', 'height=400, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
return flase;
}
-->
</SCRIPT>
<a href="#" onclick="openwin()">我要留言</a>
<a href="show.php">查看留言</a>
</body>
</html>
文件message.html
<title>留言板</title>
</head>
<body>
<form id="form1" name="form1" method="POST" action="post.php">
<label>姓名
<input type="text" name="username" />
</label>
<p>
<label>留言
<textarea rows="5" name="message" ></textarea>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
<label>
<input type="reset" name="Submit2" value="重置" />
</label>
</p>
</form>
</body>
</html>
文件post.php
<?php
$link=mysql_connect('localhost','root','123456');
mysql_select_db('stud',$link);
mysql_query("set names 'gb2312' ");
if($_POST['Submit'])
$query="INSERT INTO student(username,message)VALUES('$_POST[username]','$_POST[message]')";
if(mysql_query($query))
{echo"提交成功";
header("location:index.html");}
else
{echo"提交不成功";}
>文件show.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>結果</title>
</head>
<body>
<?php
$link=mysql_connect('localhost','root','123456');
mysql_select_db('stud',$link);
mysql_query("set names 'gb2312' ");
$result=mysql_query("select*from student",$link);
$count=mysql_num_rows($result);
$all=$count-1;
><table width="418" border="1" align="center">
<tr align="center">
<td width="130"> <?php echo"用戶名" ?> </td>
<td width="280"> <?php echo"留言內容" ?> </td>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
><tr align="center">
<td width="130"> <?php echo$row[username] ?> </td>
<td width="280"> <?php echo$row[message] ?> </td>
</tr>
<?php
}
></table>
壹***有<?php echo$all+1; ?>條留言<a href="index.html">返回首頁</a>
</body>
</html>
代碼完畢!!