因为分好几个PHP或者ASP文件弄起来很烦,我希望能够把整个留言板插入到已经做好的HTML页面里去,所以才要求用单页输入和同一个页面更新留言,而不是需要用SHOW.PHP这样的外链文件来显示留言的信息。可以的话,最好能够写一个超简单的留言本(只有名字和内容和时间就可以了)和一个较多功能的留言本(包括前3种功能外,加入表情,删除留言,修改,回复,IP显示,楼层显示) 注意,全部PHP代码都塞进一个HTML页面里,能够做到吗?
调试欢乐多
function readMessages(){
@mysql_connect('localhost', 'user', 'passwd') or die("Could not connect to MySQL Server!");
@mysql_select_db('db_name') or die("Could not select database!");
mysql_query("SET NAMES 'utf8'");//设置数据传输编码
$query = "select msgID, userID, msgtime, content from message order by msgID";
$result = mysql_query($query); if(mysql_num_rows($result)){
while(list($msgID, $userID, $msgtime, $content) = mysql_fetch_row($result)){
echo '<tr><td align="left">' . $userID . '</td><td align="right">' . $msgtime . '</td></tr><tr><td colspan="2" bgcolor="#d1d2d4">' . $content . '</td></tr>';
}
}
else {
echo "<tr><td>当前没有评论!</td></tr>";
}
mysql_close();
}
//判断用户名是否存在
function isUserExist($userID){
@mysql_connect('localhost', 'user', 'passwd') or die("Could not connect to MySQL Server!");
@mysql_select_db('db_name') or die("Could not select database!");
mysql_query("SET NAMES 'utf8'");
$query = "select userpwd from user where userID='$userID'";
$result = mysql_query($query);
//userb表中存在该用户,返回用户密码
if($result){
list($userpwd) = mysql_fetch_row($result);
return $userpwd;
}
mysql_close();
return 0;
}
//插入数据
function insert($query){
@mysql_connect('localhost', 'lizhen', 'lizhen') or die("Could not connect to MySQL Server!");
@mysql_select_db('lizhen') or die("Could not select database!");
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
mysql_close();
return $result;//返回插入数据返回的状态
}
//表单处理
if(isset($_POST['submit'])){
$userID = $_POST['userID'];
$userpwd = $_POST['userpwd'];
$content = $_POST['content'];
$msgtime = date("Y-m-d h:i:s");
$content = rtrim($content);//删除结尾的空白符
$content = strip_tags($content);//过滤html和php标记 if($userID && $userpwd && $content){
$password = isUserExist($userID);//判断用户是否存在,若存在,就返回密码,否则返回0
$arr = array("\"", "'", " ", "<", ">");
$replace = array(""", "'", " ", "<", ">");
$outcontent = str_replace($arr, $replace, $content);//如果content中存在arr中指定的字符,就把该字符替换为replace对应的字符
if(strlen(nl2br($outcontent))<60000){
$query = "insert into message(userID, msgtime, content) values ('" . $userID . "','" . $msgtime . "','" . nl2br($outcontent) . "')";
if($password){
if( strcmp($userpwd ,$password) == 0 ){
$result = insert($query);
if($result){
header("location:message.php");//提交成功,重定向到当前页
}
else echo "<script>window.alert('提交失败!'); window.location.href='message.php';</script>";
}
else echo "<script>window.alert('密码错误,请重新输入')</script>";
}
else {
$query1 = "insert into user set userID='$userID', userpwd='$userpwd'";
$result1 = insert($query1);
$result2 = insert($query); if($result1 && $result2){
header("location:message.php");
}
else echo "<script>window.alert('提交失败!'); window.location.href='message.php';</script>";
}
}
else echo "<script>window.alert('评论过长,请删减!')</script>"; }
else echo "<script>window.alert('请填写所有输入框!')</script>";
}?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h3>留言板</h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0" width="50%">
<?php readMessages();?>
</table>
<table>
<tr>
<td><textarea name="content" rows="10" cols="50" value=""></textarea></td>
</tr>
<tr>
<td>您的昵称:<input type="text" name="userID" value=""></input></td>
</tr>
<tr>
<td>密码:<input type="password" name="userpwd" value=""></input></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="提交"></input></td>
</tr>
</table>
</form>
</body>
</html>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>留言版</title>
</head><body><form name="form1" method="post" action="insert.php">
<p>你的名字:<input type="text" name="name" size="20" /></p>
<p>你的性别:<input type="radio" value="1" name="sex" checked="checked" />男生 <input type="radio" value="0" name="sex" />女生 </p>
<p>你的E-mail:<input type="text" name="email" size="20" /></p>
<p>你的留言内容:</p>
<p><textarea rows="9" name="info" cols="35"></textarea></p>
<p><input type="submit" value="提交" name="B1" /><input type="reset" value="重设" name="B2" /></p>
</form></body></html>++++++++++++++++++++++++++++++++++++++++++++ ADD.php<?php
$mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "123123";
$mysql_database = "gbook";
$ip = getenv('REMOTE_ADDR');
$conn = mysql_connect("localhost","root","123123");
mysql_select_db("gbook");
$sql = "INSERT INTO `gbook` ( `id` , `name` , `sex` , `email` , `info` , `ip` , `time_at` )
VALUES (NULL , '$name', '$sex', '$email', '$info', '$ip', NOW( ))";
$result = mysql_query($sql,$conn);
$id = mysql_insert_id();
mysql_close($conn);
?>
<p>留言成功</p>
<p><a href="show.php">去留言页 </a></p>+++++++++++++++++++++++++++++++++++++show.php<?
$mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "123123";
$mysql_database = "gbook";
$sql = "SELECT * FROM gbook ORDER BY 'id' DESC"; //排序 后留言的在前面显示 $conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{ // ----if语句判断男女------
if($row[2]==1)
{ $gender = '男';}
else
{ $gender = '女'; }
?><table width="752" border="1">
<tr>
<td height="32"><p><?=$row[6]?> <?=$row[5]?> </p>
<p><?=$row[1]?>(<?=$gender?>) <?=$row[3]?></p></td>
</tr>
<tr>
<td height="45"><?=nl2br($row[4])?><p><a href="change.php?id=<?=$row[0]?>">[修改]</a> <a href="del.php?id=<?=$row[0]?>">[删除]</a></p></td>
</tr>
</table>
<hr /><?
}
mysql_free_result($result);
?>***************************以下是报错******************Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in D:\www\insert.php on line 10Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\www\insert.php on line 11Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in D:\www\insert.php on line 11Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\www\insert.php on line 14Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in D:\www\insert.php on line 15Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in D:\www\insert.php on line 16
执行查询语句失败了!!!还有下面的那个SQL语句也失败了
$result = @mysql_query("select * from userinfo")or die("执行SQL语句失败!");
//执行SQL语句 $sql="insert into gbook( id , name , sex , email , info , ip, time_at ) values
(NULL,'$name','$sex','$email','$info','$ip', now( ))";
(NULL,'$name','$sex','$email','$info','$ip', now( ))";
这个id是自增的么?是主键么?如果是的话那么将NULL改成default
PRIMARY KEY (id)
现在把内容写入数据库的程序已经成功了(自己弄好了),就是不知道怎么显示到PHP页面来,让大家看见。的时候出问题,说
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Warning: mysql_mysql_fetch_array(): supplied argument is not a valid MySQL result resource
<html>
<head>
<script type="text/javascript" src="../jquery/jquery.js" tppabs="http://www.w3school.com.cn/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#触发的媒介").click(function(){
$('#变化的元素').load('程序页面');
});
});
</script>
</head>
<body><div id="变化的元素"><h2>程序页面</h2></div>
<button id="触发的媒介" type="button">Change Content</button></body>
</html>