<?php
include "02.php";
$sql="select count(*) from liu";
$result=mysql_query($sql,$conn);
$row=mysql_fetch_row($result);
echo $row[0];
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>留言板</title>
<script language="javascript">
function test(){
if(myform.title.value==""){
alert("标题不能为空!");
return false;
}else{
$sum=myform.title.value.length;
if($sum<5 || $sum>20){
alert("请填写5-20个字符");
return false;
}
}
if(myform.name.value==""){
alert("名字不为空!");
return false;
}else{
$sum=myform.name.value.length;
if($sum<2 || $sum>10){
alert("请填写2-10个字符");
return false;
}
}
if(myform.content.value==""){
alert("内容不为空!!");
return false;
}else{
$sum=myform.content.value.length;
if($sum<10){
alert("内容不能小于10");
return false;
}
}
return true;
}
</script>
</head>
<body>
<form action="01.php" method="post" name="myform" onsubmit="return test();">
<table width="500" height="30" cellpadding=0 cellspacing="0" border="0">
<h2>发表留言</h2>
<tr>
<td align="right">留言标题:</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="right">内容:</td>
<td>
<textarea cols="40" rows="5" name="content"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include "02.php";
$title=magic($_POST['title']);
$name=magic($_POST['name']);
$content=magic($_POST['content']);
$time=date("Y-m-d H:i:s");
function magic($str){
$str=trim($str);
if(!get_magic_quotes_gpc()){
$str=addslashes($str);
}
return htmlspecialchars($str);
}
echo $title.$name.$content;
$sql="insert into liu(title,name,content,time) values('$title','$name','$content','$time')";
$isok=mysql_query($sql,$conn);
if($isok){
echo "执行成功";
}else{
echo "执行失败";
}
<?php
$conn=mysql_connect('localhost','root','');
if(!$conn){
echo "连接失败";
}
$sql="use test";
mysql_query($sql,$conn);
$sql="set names utf8";
mysql_query($sql,$conn);