首页 > 代码库 > php+mysql 最简单的留言板

php+mysql 最简单的留言板

学完了记得动手操作。

測试地址(未过滤)

<html>
<body>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<form method = "post" action = "post.php">
欢迎来到我的留言板!

<br> name:<input type = "text" name = "name"><br> email:<input type = "text" name = "email"><br> content:<br> <textarea rows="10" cols="20" name="content"> </textarea> <input type = "submit"> </form> </body> </html>


<?php 
header("Content-type: text/html; charset=utf-8"); 
$con = mysql_connect("***","***","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
//connect table
  mysql_query("SET NAMES utf8");
 mysql_select_db("message",$con);

$sql = "CREATE TABLE message(
id int NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
name varchar(15),
email varchar(15),
content varchar(100)
 )";
//mysql_query($sql,$con);

$name =  $_POST['name'] ;
$email =  $_POST['email'];
$content = $_POST['content'];

$sql1 = "INSERT INTO message(name,email,content)
VALUES( '$name', '$email','$content')";

mysql_query($sql1,$con);

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
<th>content</th>
</tr>";

$result = mysql_query("SELECT * FROM message");

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['content'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

中途遇到一个bug

mysql_fetch_array(): supplied argument is not a valid MySQL result resource

定位到该函数。$result的值不是有效的。

怀疑是数据库没有连接成功。

推断一下$result。输出mysql_error();

发现是没有数据库选择。

原来放在本地能够运行的,原来放到站点上数据库名称写成表单名称了。

蠢哭。

= =!!

php+mysql 最简单的留言板