博客 |蓝易论坛 |加入收藏 | 设为首页 | RSS
您当前的位置:首页 > 网络与编程 > php

最简单的php+myql留言本

时间:2009-01-12 12:22:44  来源:  作者:

最简单的php+myql留言本

可供php入门参考。
由三个文件组成,分别是comments.php guestbook.php 和 store_comment.php

首先当然是导入数据表,通过phpmyadmin导入,如果是本机测试的话,http://localhost/phpmyadmin登录测试

CREATE TABLE guestbook (
id int(11) NOT NULL auto_increment,
name text NOT NULL,
email varchar(80) NOT NULL default '',
v_comment varchar(255) default NULL,
country varchar(40) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

下来建立输入留言的表单,保存文件名为comments.php

<form name="form1" method="post" action="store_comment.php">
Your Name: <input name="name" type="text" size="15"> <br>
Your Email ID: <input name="email" type="text" size="15"> <br>
Country: <input name="country" type="text" size="15"> <br>
Comments : <textarea name="comments" cols="15" rows="3"></textarea>
<br>
<input type="submit" name="Submit" value="Submit">
</form>

接着提交存储,保存为store_comment.php名的文件

<? // code for store_comment.php

// Database information required to connect to database
$host="localhost";
$name = "username";
$pass = "password";
$dbname = "databasename";

// Connect to Database and select the database to use
$dbi = mysql_connect($host, $name,$pass) or
die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

// Get the values posted from the Form in comments.php
$name = $_POST["name"];
$email = $_POST["email"];
$country = $_POST["country"];
$comment = $_POST["comments"];

//Check if a name & comment have been entered

if ($name=="")
{
die (" You haven't Entered Your Name. Go back and Enter your Name");
}

if ($comment=="")
{
die (" You haven't Entered any comment. Go back and enter some comment to be stored");
}

// Insert the Details into the Database
$sql = "INSERT INTO guestbook (name,email,v_comment,country) VALUES ('$name','$email','$comment','$country')";
$result = mysql_query($sql,$dbi);
If ($result)
{
echo "<center> Your Details have been added to the database<br>";
echo " <a href="guestbook.php">Click here to go back to the guestbook</a></center>" ;
} else
echo " Your details were not added due to some database problem";

?>

最后显示留言,保存为guestbook.php

<? // Source for guestbook.php
// Database information required to connect to database
$host="localhost";
$name = "username";
$pass = "password";
$dbname = "databasename";

// Connect to Database and select the database to use
$dbi = mysql_connect($host, $name,$pass) or
die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

// Display the top of the page for the guestbook
echo " <center><h1> A Simple Guestbook in PHP & MYSQL </h1></center><br>";
echo " <a href="comments.php">Click here to Leave us a message</a><br><br>";
echo " <b>Comments from Visitors </b><hr>";

// Fetch and Display the Results from the Database
$result = mysql_query("select name, email, country, v_comment from guestbook ORDER BY id",$dbi);

while ($myrow = mysql_fetch_array($result))
{
echo "<b>name:</b> $myrow[0]<br> <b>email: </b>$myrow[1]<br> <b>Country:</b> $myrow[2] <br><b>message:</b> $myrow[3] ";
}

echo("<hr> This guest book is coded in PHP and uses MySQL to store data ");

?>

这样在浏览器中输入http://localhost/存放文件夹名/guestbook.php
就可以看见留言本了。

来顶一下
近回首页
返回首页
下一篇:方便的服务器端解压工具phpzip 上一篇:windows平台下的apache+php+mysql套件vertrigoserv

正在载入评论数据中...
用户名: 验证码: 游客请勾选
         
分类
  • 网络
  • joomla专栏
  • php
  • ftp工具
  • 网站建设
  • 推荐资讯
    栏目更新
    栏目热门