php+mysql实例教程:简单留言本

首页电脑网络php+mysql实例教程:简单留言本(2012年05月28日)

以前记得玩ASP+ACCESS的时候,就是弄blog和通过简单实例,慢慢慢慢学会了倒腾简单的ASP+ACCESS程序。

现在转到了PHP+MYSQL,这么着也得稍微了解点PHP+MYSQL编程啊,虽然目前修改下模板什么的问题不大,但是还是希望能够了解下PHP+MYSQL编程,自己写个在线电话号码本用用也不错,呵呵

转悠了下还真不好找这种简单的,这不,找到了,就贴上来放着,高手略过啊。

这是一个PHP+MYSQL的留言本的实例:

1、head.php

<head>
	<meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
	<title></title>
	<b><a href = "add.php"> 添加留言</a> | <a href = "list.php"> 浏览留言</a><b>
	<hr size = 1>
</head>

这个大家基本可以忽略不计了吧。
2、conn.php 重要的数据库连接的文件。

<?php
	$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
    mysql_select_db("bbs", $conn);
    mysql_query("set names 'GBK'"); //使用GBK中文编码;
 
	function htmtocode($content) { //将中php的空格换行改成html模式
	$content = str_replace("\n", "<br>", str_replace(" ", "&nbsp;", $content));
	return $content;
}
?>

主要是这个数据库连接的基本格式:

$conn = @ mysql_connect(“主机名”, “用户名”, “密码”) or die (“链接错误”); 链接不上mysql则显示“链接错误”
mysql_select_db(“数据库名”, $conn);
php链接到mysql中的选择语句解决中文乱码mysql_query(“set names ‘GBK’”);

3、add.php 向数据库添加留言记录

<?php
	include("conn.php");
	include("head.php");
	if($_POST['submit']){
		$sql = "insert into message (id, user, title, content, lastdate) " .
		    "values ('', '$_POST[user]', '$_POST[title]', '$_POST[content]', now())";
		echo "<script language=\"javascript\">alert('添加成功');history.go(-1)</script>";
		//输出“添加成功”  并且返回上一个页面
	}
	mysql_query($sql);
?>
 
<script type = "text/javascript">
	function CheckPost()
	{	
		if(myform.user.value == "")
		{
			alert("请填写用户名");
			myform.user.focus();//光标移到user
			return false;
		}
		if(myform.title.value.length < 2)
		{
			alert("标题不能少于2个字符");
			myform.title.focus();
			return false;
		}
		if(myform.content.value == "")
		{
			alert("请填写留言内容");
			myform.content.focus();
			return false;
		}
	}
</script>
 
<form action = "add.php" method = "post" name = "myform" onsubmit = "return CheckPost()">
	用户:<input type = "text" size = "10" name = "user"/><br>
	标题:<input type = "text" size = "10" name = "title"/><br>
	内容:<textarea name = "content" cols = "60" rows = "10"></textarea><br>
	<input type = "submit" name = "submit" value = "发布留言"/>
 
</form>

4、list.php 就是显示留言和分页了

<?php
	include("conn.php");
	include("head.php");
	$pagesize = 5;
	$url = $_SERVER["REQUEST_URI"];
	//print_r($url);echo "<br/>";
	$url = parse_url($url);//print_r($url);echo "<br/>";
	$url = $url[path];//echo print_r($url);echo "<br/>";
 
	$numq = mysql_query("SELECT * FROM `message`");
	$num = mysql_num_rows($numq);
//	echo $num;
//	$num = mysql_num_fields($numq);
//	echo $num;
 
	if($_GET[page]){
		$pageval = $_GET[page];
		$page = ($pageval - 1) * $pagesize;
		$page.=',';
	}
 
	if($num > $pagesize){
		if($pageval <= 1) $pageval = 1;
		$temp = $num / $pagesize;
		if($num % $pagesize != 0) $temp++;
		if($pageval >= $temp - 1){
			echo "共 $num 条".
				" <a href = $url?page=".($pageval - 1).">上一页</a>";
		}
		else if($pageval >= 2){
			echo "共 $num 条".
				" <a href=$url?page=".($pageval - 1).">上一页</a> <a href = $url?page=".($pageval + 1).">下一页</a>";
		}
		else{
			echo "共 $num 条".
				" <a href = $url?page=".($pageval + 1).">下一页</a>";
		}
		echo "<hr>";
	}
?>
 
<table width = 500 border = "0" align = "center" cellpadding = "5" cellspacing = "1" bgcolor = "#add3ef">
	<?
		$sql = "select * from message limit $page $pagesize";
			$query = mysql_query($sql);
			while($row = mysql_fetch_array($query)){
	?>
		<tr bgcolor = "#eff3ff">
		<td>标题:<?= $row[title]?> </td>
		<td>用户:<?= $row[user] ?> </td>
		</tr>
		<tr bgcolor = "#ffffff">
		<td>内容:<?= htmtocode($row[content]) ?></td>
		<td></td>
		</tr>
	<?
	}
	?>
</table>

这个就是千辛万苦找来的最简单的PHP+MYSQL实例了,够简单吧。

添加新评论 »

You must enable javascript to see captcha here!