Home >php教程 >php手册 >php+mysql 最简单的留言板

php+mysql 最简单的留言板

WBOY
WBOYOriginal
2016-06-21 08:46:21777browse

学完了记得动手操作。



欢迎来到我的留言板!
name:
email:
content:
 

<!--?php 
header(Content-type: text/html; charset=utf-8); 
$con = mysql_connect(***,***,***);
if (!$con)
  {
  die(&#39;Could not connect: &#39; . 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[&#39;name&#39;] ;
$email =  $_POST[&#39;email&#39;];
$content = $_POST[&#39;content&#39;];

$sql1 = INSERT INTO message(name,email,content)
VALUES( &#39;$name&#39;, &#39;$email&#39;,&#39;$content&#39;);

mysql_query($sql1,$con);

echo <table border=&#39;1&#39;-->

id
name
email
content
;

$result = mysql_query(SELECT * FROM message);

while($row = mysql_fetch_array($result))
  {
  echo ;
  echo  . $row[&#39;id&#39;] . ;
  echo  . $row[&#39;name&#39;] . ;
  echo  . $row[&#39;email&#39;] . ;
  echo  . $row[&#39;content&#39;] . ;
  echo ;
  }
echo ;

mysql_close($con);
?>

中途遇到一个bug

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

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

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

判断一下$result,输出mysql_error();

发现是没有数据库选择。

原来放在本地可以执行的,原来放到网站上数据库名称写成表单名称了。



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn