conn.php:<?php $conn=@mysql_connect("localhost","root",""); mysql_select_db("bbs",$conn); mysql_query("set names'utf-8'"); if(!$conn){ die('数据库连接失败'.mysql_error()); } echo "数据库连接成功"; function htmtocode($content){ $content=str_replace("\n","<br>",str_replace(""," ",$content)); return $content;}?>add.php:<?php include("conn.php"); include("head.php"); if(isset($_POST['submit']) && $_POST['submit']) { $sql="insert into message(id,user,title,content,lastdate) values('','$_POST[user]','$_POST[title]','$_POST[content]',now())"; if(mysql_query($sql)){ echo"<center>发布成功</center>"; } else echo "<center>上传失败<center>"; }?> <SCRIPT language=javascript> function CheckPost(){ if(myform.user.value=="") { alert("请填写用户名"); myform.user.focus(); return false; } if(myform.title.value.length<5) { alert("标题不能少于5个字符"); myform.title.focus(); return false; } if(myform.content.value=="") { alert("必须要填写留言内容"); myform.content.focus(); return false; } }</SCRIPT>
修改一个小地方:
conn.php
<?php
$conn=@mysql_connect("localhost","root","");
mysql_select_db("bbs",$conn);
mysql_query("set names'utf-8'");
if(!$conn){
die('数据库连接失败'.mysql_error());
}
else echo "数据库连接成功";
function htmtocode($content){
$content=str_replace("\n","<br>",str_replace(""," ",$content));
return $content;}
?>
include("conn.php");
include("head.php");
if(isset($_POST['submit']) && $_POST['submit']) {
$sql="insert into message(id,user,title,content,lastdate) values('','$_POST[user]','$_POST[title]','$_POST[content]',now())";
echo $sql; //先看看你生成的sql语句 是否正常 最好在PHPMYADMIN里面 或者 提示行模式下测试一下
if(mysql_query($sql)){
echo"<center>发布成功</center>";
}
else echo "<center>上传失败<center>";
}
?>