<!--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
include("conn.php");
if('$_POST[submit]'){
$sql="INSERT INTO message (user,title,content,lastdate) VALUES ('$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo("成功!");
}
?>
<form action="add.php" method="post">
用户:
<input type="text" size="10" name="user" />
<br />
标题:
<input type="text" name="title" />
<br />
内容:
<textarea name="content"></textarea>
<br />
<input type="submit" value="发布留言" />
</form><!--/body>
</html-->
为什么把HTML代码注释起来才可以添加到MySQL数据库里,注释起来之后页面显示乱码。
如果不注释的话添加不到数据库里。
<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
include("conn.php");
if('$_POST[submit]'){
$sql="INSERT INTO message (user,title,content,lastdate) VALUES ('$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo("成功!");
}
?>
<form action="add.php" method="post">
用户:
<input type="text" size="10" name="user" />
<br />
标题:
<input type="text" name="title" />
<br />
内容:
<textarea name="content"></textarea>
<br />
<input type="submit" value="发布留言" />
</form><!--/body>
</html-->
为什么把HTML代码注释起来才可以添加到MySQL数据库里,注释起来之后页面显示乱码。
如果不注释的话添加不到数据库里。
include("conn.php");
if($_SERVER["REQUEST_METHOD"] == "POST"){
$sql="INSERT INTO message (user,title,content,lastdate) VALUES ('$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo("成功!");
}
?>
把HTML头和尾注释掉之后就可以添加到数据库里了,但页面就成乱码了!
打印insert语句,还有显示mysql_error()信息
“鎴愬姛!
鐢ㄦ埛锛�
鏍囬锛�
鍐呭锛”�
这样的乱码。如何才能使页面运行和显示显示正常。
出现的问题是:运行正常显示不正常,显示正常运行不正常!
显示正常的时候运行还不报错,就是不会把表单数据提交到数据库!
include("conn.php");
if($_POST["submit"]){
$sql="INSERT INTO message (user,title,content,lastdate) VALUES ('$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
echo("成功!");
}
?>
<!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 action="add.php" method="post">
用户:
<input type="text" size="10" name="user" />
<br />
标题:
<input type="text" name="title" />
<br />
内容:
<textarea name="content"></textarea>
<br />
<input type="submit" name="submit" value="发布留言" />
</form></body>
</html>
<?php....?>应该放到最前面位置。否则_Post这类值是无法被正确取得的。<?php
include("conn.php");
if('$_POST[submit]'){
$sql="INSERT INTO message (user,title,content,lastdate) VALUES ('$_POST[user]','$_POST[title]','$_POST[content]',now())";
mysql_query($sql);
$flag = true;
//echo("成功!");
}
?>
<!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 action="add.php" method="post">
用户:
<input type="text" size="10" name="user" />
<br />
标题:
<input type="text" name="title" />
<br />
内容:
<textarea name="content"></textarea>
<br />
<input type="submit" value="发布留言" />
<? if($flag) echo "成功";?>
</form></body>
</html>
他自己不显示,你就让他输出啊,echo。。要不你把你代码拆开来,第一页放form那段,第二页放数据库操作,操作完再location.href(url)回到第一页。