php留言板程序(适合php初学者)(1/4)

 更新时间:2016年11月25日 16:39  点击:1328
这是一款简单的php留言板程序代码,如果你正学习网站开发,这款留言板源码可以帮助哦,希望本文章对你有帮助。
 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk" />
<title>所有留言</title>
<link rel="stylesheet" type="text/css教程" href="style.css" media="all" />
</head>

<body>
<a href="add.php">发表留言</a>
<?php
require('common.php');
$result = mysql教程_query("select * from gb_content order by id desc");//查询数据

while ($row = mysql_fetch_array($result, mysql_both)) {// 取一条数据
?>
<table width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
  <tr>
    <td class="bg"><b>[<?php echo htmlentities($row['username'],ent_compat,'utf-8') ?>]</b> 发表于:<?php echo htmlentities($row['insert_time'],ent_compat,'utf-8') ?></td>
  </tr>
  <tr>
    <td><?php echo htmlentities($row['content'],ent_compat,'utf-8') ?></td>
  </tr>
  <tr>
    <td align="right"><a href="edit.php?id=<?php echo $row['id'] ?>">修改</a> <a href="delete.php?id=<?php echo $row['id'] ?>">删除</a></td>
  </tr>
</table>
<?php
}

mysql_free_result($result);

?>
</body>
</html>

 

 

 代码如下 复制代码

class dividepage{//分页类
 private $total;//要显示的总记录数
 private $url;//请求的url地址
 private $displaypg;//每页显示的记录数,默认为每页显示10条记录
 private $page;//当前页码
 private $lastpg;//总页数,即最后一页的页码
 private $prepg;//前一页
 private $nextpg;//后一页
 private $firstcount;//记录条数开始的序号从0开始
 private $startd;//记录条数开始的记录号.
 private $stopd;//记录条数结束的记录号.

//构造函数
public function __construct($url, $total, $displaypg){
 $this->url = $url;//请求的url
 $this->total = $total;//总记录数
 //if($displaypg == '')
 $this->displaypg = $displaypg;//每页显示的记录数
 $this->initdividepage();//初始化分页类
 //echo ','.$this->displaypg;
}

//初始化分页类
private function initdividepage(){
 //分析url
 $parse_url = parse_url($this->url);//将url解释为有固定键值对的数组
 $url_query = $parse_url['query'];//取出url中的查询字符串
 if($url_query){//如果有查询字符串,则删除查询字串中当前页的查询字段如:&page=$page或page=$page
  ereg('(^|&)page=([0-9]*)', $url_query, $k);
  $this->page = $k[2];//取得当前页的值
  $url_query = ereg_replace("(^|&)page=$this->page", '', $url_query);//删除查询字串中当前页的查询字段如:&page=$page或page=$page
  $this->url = str_replace($parse_url['query'], $url_query, $this->url);//保留其他的查询字串,
  $this->page = $this->page ? $this->page : 1;//w如果查询字符串中没有当前页的值就设当前页为1
  if($url_query){//如果有其他查询字符串,则以&page=$page形式添加翻页查询字串
   $this->url .= '&page';
  }else{//如果没有其他查询字串,则以page=$page形式添加翻页查询字串
   $this->url .= 'page';
  }
 }else{//如果没有查询字串,则在url后添加?page=$page形式的翻页查询字串
  $this->page = 1;
  $this->url .= '?page';
 }
 $this->lastpg = ceil($this->total / $this->displaypg);//计算总页数,即最后一页的页码
    $this->page = min($this->lastpg, $this->page);//如果当前页大于总页数,则当前页为最后一页的页码
    $this->prepg = $this->page - 1;//上一页为当前页减一www.111cn.net
    $this->nextpg = $this->page + 1;//(($this->page == $this->lastpg) ? $this->lastpg : ($this->page + 1));//下一页为当前页加一,如果当前页为最后一页,则下一页为0
    $this->firstcount = ($this->page - 1) * $this->displaypg;//计算当前页,记录条数开始的记录号,从0开始.
 $this->startd = $this->total ? ($this->firstcount + 1) : 0;//记录开始号从1开始
 $this->stopd = min($this->firstcount + $this->displaypg, $this->total);//记录结束号
 //echo $this->displaypg;
 //echo $this->nextpg.'+=+='.$this->lastpg;
}

public function getpageinfo(){//取得当前页面的基本信息,如:显示第 1-10 条记录,共 23 条记录。
 return '<span class="pageinfostyle">显示第<span class="numstyle">'.$this->startd.'-'.$this->stopd.'</span>条记录,共<span class="numstyle">'.$this->total.'</span>条记录。</span>';
}

public function getcommonpagenav(){//取得通常的分页导航,如:首页 上一页 下一页 尾页
 $commonnav = '';
 if($this->lastpg == 1){//如果只有一页,则返回翻页导航,退出,不显示下一页,上一页等。。。
  return $commonnav;
  break;
 }
 $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首页</a>';//设置首页导航,page=1
 if($this->prepg){
  $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一页</a>';
 }else{
  $commonnav .= '<a class="fcompagestyle">上一页</a>';
 }
 if($this->nextpg <= $this->lastpg){
  $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一页</a>';
 }else{
  $commonnav .= '<a class="fcompagestyle">下一页</a>';
 }
 $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾页</a>';//显示尾页链接
 return $commonnav;
}

//取得跳转分页导航,如:第n页
public function getjumppagenav(){
 //<select name='topage' size='1' onchange='window.location="/test/page.php?page="+this.value'>
 $jumpnav = '<span class="pageinfostyle">到第<select name="topage" size="1" class="topage"  onchange='window.location="'.$this->url.'="+this.value'>'." ";
 for($i = 1; $i <= $this->lastpg; $i++){
  if($i == $this->page){//把当前页的页码作为默认选项
   $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." ";
  }else{
   $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." ";
  }
 }
 $jumpnav .= '</select>页,共<span class="numstyle">'.$this->lastpg.'</span>页</span>';
 return $jumpnav;
}

//取得所有的分页导航
public function getallpagenav(){
 $temp =  $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav();
 return $temp;
}

//取得当前页需显示的记录,在数据库教程中的限定范围,如0-9
public function getlimitstr(){
 //echo $this->page;
 //echo $this->firstcount;
 
 //echo $this->dispalypg;
 $temp = $this->firstcount.','.$this->displaypg;
 //echo $temp;
 return $temp;
}

}

 

/*
使用实例:

 代码如下 复制代码
*$result=mysql教程_query("select * from tb_pagetest");//从数据库中查询所需显示的数据
*$total=mysql_num_rows($result);//查询到的数据的总条数
*$pagesize = 5;//每页显示的记录条数
*$url = $_server['request_uri'];//请求的uri
*
*$dividepageclass = new dividepage($url, $total, $pagesize); //创建分页类,(类能自动初始化)
*$limitstr = $dividepageclass->getlimitstr();//取得当前页要显示的记录开始序号和每页显示条数,如:0, 5(显示从0开始的5条记录)
*echo $dividepageclass->getallpagenav();//显示所有分页导航条,
*如:显示第11-13条记录,共13条记录。首页 上一页 下一页 尾页 到*第 1 页,共 3 页
*$sql = 'select * from tb_pagetest limit '.$limitstr;
*$result=mysql_query($sql);//从数据库中取得当前页要显示的记录集,然后显示就ok
*如:
*while($row=mysql_fetch_array($result))
*echo "<hr><b>".$row[title]." | ".$row[author];

 

 

 代码如下 复制代码

$pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes")
or die("can't connect to database.");
$query="select * from employes order by serial_no";

//$query="insert into employes values(10008,'susan','1985-09-04','80','50')";
$result=@pg_query($pg,$query) or die("can't run query to table.");
//echo pg_num_rows($result); //输出多少条记录被查询
//if($result)
//{
//echo "recrods inserted sucessfully!";
//echo pg_affected_rows($result);//输出多少条记录被插入
//}

//实例一[pg_fetch_row]
echo "<table border=1>";
echo "<tr>";
echo "<td>serial_no</td>";
echo"<td>name</td>";
echo"<td>birthday</td>";
echo"</tr>";

for($i=0;$i<pg_num_rows($result);$i++)
{

$row=@pg_fetch_row($result) or die("can't fetch row from table.");
$serial_no= $row[0];
$name= $row[1];
$birthday= $row[2];
echo"<tr>";
echo"<td>$serial_no</td>";
echo"<td>$name</td>";
echo"<td>$birthday</td>";
echo"</tr>";

}
echo"</table>";

//实例二[pg_fetch_array]
//echo "<table border=1>";
//echo "<tr>";
//echo "<td>serial_no</td>";
//echo"<td>name</td>";
//echo"<td>birthday</td>";
//echo"</tr>";
//
//for($i=0;$i<pg_num_rows($result);$i++)
//{
//
//$row=@pg_fetch_array($result) or die("can't fetch row from table.");
//$serial_no= $row['serial_no'];
//$name= $row['name'];
//$birthday= $row['birthday'];
//echo"<tr>";
//echo"<td>$serial_no</td>";
//echo"<td>$name</td>";
//echo"<td>$birthday</td>";
//echo"</tr>";
//
//}
//echo"</table>";

//增加,删除,修改实例
//$newrow=array("serial_no"=>"1006","name"=>"peter","birthday"=>"1990-07-03","salary"=>"90","bonus"=>"80");
//$reusult=@pg_insert($pg,"employes",$newrow) or die("can't insert data to table.");
//if($reusult)
//{
//echo "rechords inserted sucessfully!";
//}
//
pg_close($pg);

本款php连接mysql数据库连接程序代码是一款比较简单实用的连接代码,希望本教程对各位同学会有所帮助哦。
 代码如下 复制代码

class mysql {
      public $sqlserver = 'localhost';
      public $sqluser = 'root';
      public $sqlpassword = '';
      public $database;

      public $last_query = '';

      private $connection;
      private $query_result;

      public function __construct() {}

      public function __destruct() {
       $this->close();
      }

    //+======================================================+
    // create a connection to the mysql database
    //+======================================================+
    public function connect($server = null, $user = null, $password = null, $database = null){
     if (isset($server)) $this->sqlserver = $server;
     if (isset($user)) $this->sqluser = $user;
     if (isset($password)) $this->sqlpassword = $password;
     if (isset($database)) $this->database = $database;

    $this->connection = mysql_connect($this->sqlserver, $this->sqluser, $this->sqlpassword);
    if($this->connection){
    if (mysql_select_db($this->database)){
    return $this->connection;
    }else{
    return $this->error();
    }
    }else{
    return $this->error();
    }
    }
    //+======================================================+
    // execute a query
    //+======================================================+
    public function query($query, $die = false){
      if ($query != null){
       $this->last_query = $query;
       $this->query_result = mysql_query($query, $this->connection);
       if(!$this->query_result){
        if ($die) die("die: ".$this->query_result);
        return $this->error();
       }else{
        if ($die) die("die: ".$this->query_result);
        return $this->query_result;
       }
      }else{
       echo "empty query cannot be executed!";
      }
    }
    //+======================================================+
    // returns the result
    //+======================================================+
    public function getresult(){
       return $this->query_result;
    }
    //+======================================================+
    // returns the connection
    //+======================================================+
    public function getconnection(){
       return $this->connection;
    }
    //+======================================================+
    // returns an object with properties rep
    //     resenting the result fields www.111cn.net and values
    //+======================================================+
    public function getobject($qry = null){
     if (isset($qry)) $this->query($qry);
     return mysql_fetch_object($this->getresult());
    }
    //+======================================================+
    // returns an array with keys representi
    //     ng the result fields and values
    //+======================================================+
    public function getarray($query_id = ""){
      if($query_id == null){
       $return = mysql_fetch_array($this->getresult());
      }else{
       $return = mysql_fetch_array($query_id);
      }
      return $return ? $return : $this->error();
    }
    //+======================================================+
    // returns the number of rows in the res
    //     ult
    //+======================================================+
      public function getnumrows($qry = null){
       if (isset($qry)) $this->query($qry);
       $amount = mysql_num_rows($this->getresult());
       return empty($amount) ? 0 : $amount;
    }
    //+======================================================+
    // returns if the result contains rows
    //+======================================================+
    public function hasresults($qry = null) {
      if (isset($qry)) $this->query($qry);
     return $this->getnumrows($qry) > 0;
    }
    //+======================================================+
    // returns the number of rows that where
    //     affected by the last action
    //+======================================================+
    public function getaffectedrows($qry = null, $query_id = null){
      if (isset($qry)) $this->query($qry);
      if(empty($query_id)){
       $return = mysql_affected_rows($this->getresult());
      }else{
       $return = mysql_affected_rows($query_id);
      }
      return $return ? $return : $this->error();
    }
    //+======================================================+
    // returns the auto generated id from th
    //     e last insert action
    //+======================================================+
    public function getinsertid($connection_link = null){
    return mysql_insert_id(isset($connection_link) ? $connection_link : $this->connection);
    }
    //+======================================================+
    // close the connection to the mysql dat
    //     abase
    //+======================================================+
    public function close(){
    if(isset($this->connection)){
    return @mysql_close($this->connection);
    }
    else {
     return $this->error();
    }
    }
    //+======================================================+
    // outputs the mysql error
    //+======================================================+
    private function error(){
    if(mysql_error() != ''){
    echo '<b>mysql errorwww.111cn.net</b>: '.mysql_error().'<br/>';
    }
    }
    }

    //demo
    // database object initialization
$db = new mysql();
$db->connect("localhost", "root", "123456", "user");

// update query
//$db->query("update table_name set field_name = value where another_field = another_value");

// select with check for record amount

if ($db->hasresults("select * from userinfo")) {
//   loop through the user records, and get them as objects
//   note that the getobject method will use the last executed query when not provided with a new one
  while ($user = $db->getobject()) {
    echo "user $user->username is called $user->password<br /> ";
  }
}
else {
  echo "no results where found";
}

本程序实现数据导入原理是先把csv文件上传到服务器,然后再通过php的fopen与fgetcsv文件把数据保存到数组,然后再用while把数据一条条插入到mysql数据库
 代码如下 复制代码
$fname = $_files['myfile']['name'];
$do = copy($_files['myfile']['tmp_name'],$fname);
if ($do){
echo"导入数据成功<br>";
}else{
echo "";
}

 

 代码如下 复制代码

error_reporting(0);// 导入csv格式的文件
$connect=mysql_connect("localhost","root","") or die("could not connect to database");
mysql_select_db("gklqtzcx",$connect) or die (mysql_error());
mysql_query("set names 'gbk'");
$fname = $_files['myfile']['name'];
$handle=fopen("$fname","r");
while($data=fgetcsv($handle,10000,",")){
$q="insert into records (name,classes,a_time,college,notify,receiver,r_time,handler) values ('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')";
mysql_query($q) or die (mysql_error());
}
fclose($handle);
echo "<meta http-equiv="refresh" content="1;url=list.php">1秒钟转入列表页,请稍等."

?>
<form enctype="multipart/form-data" action="<?php echo"".$_server["php_self"].""; ?>" method="post">
<p>导入cvs数据 <input name="myfile" type="file"> <input value="提交" type="submit">
</p>
</form>

[!--infotagslink--]

相关文章

  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • node.js+express留言板功能实现示例

    本文介绍基于nodejs+express+art-template的留言板功能。包含列表界面、添加界面和发送留言功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-21
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • 微信小程序手势操作之单触摸点与多触摸点

    这篇文章主要介绍了微信小程序手势操作之单触摸点与多触摸点的相关资料,需要的朋友可以参考下...2017-03-13
  • 微信小程序(应用号)开发新闻客户端实例

    这篇文章主要介绍了微信小程序(应用号)开发新闻客户端实例的相关资料,需要的朋友可以参考下...2016-10-25
  • Python爬取微信小程序通用方法代码实例详解

    这篇文章主要介绍了Python爬取微信小程序通用方法代码实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-09-29
  • 微信小程序 页面跳转传递值几种方法详解

    这篇文章主要介绍了微信小程序 页面跳转传递值几种方法详解的相关资料,需要的朋友可以参考下...2017-01-16
  • php实现网站留言板功能

    我要实现的就是下图的这种样式,可参考下面这两个网站的留言板,他们的实现原理都是一样的畅言留言板样式:网易跟帖样式:原理 需要在评论表添加两个主要字段 id 和 pid ,其他字段随意添加,比如文章id、回复时间、回复内容、...2015-11-08
  • 微信小程序实现点击导航条切换页面

    这篇文章主要为大家详细介绍了微信小程序实现点击导航条切换页面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-11-19
  • 微信小程序实现登录页云层漂浮的动画效果

    微信小程序目前的火热程度相信不用多言,最近利用空余时间用小程序实现了个动态的登录页效果,所以下面这篇文章主要给大家介绍了利用微信小程序实现登录页云层漂浮动画效果的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2017-05-09