PHP教程

PHP+Wampserver(其中的MariaDB数据库)制作留言板

本文主要是介绍PHP+Wampserver(其中的MariaDB数据库)制作留言板,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、准备

笔记本电脑·、Notepad++编辑器、Wampserver、Navicat连接MariaDB数据库。

二、实现代码

index.php

<html>/**
这里是注册与登陆页面
小zhangQQ:205187370**/
<head>
<title>登陆</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./css/index1.css">//页面布局
<style>
body{height:100%;}
</style>
</head>
<body style="background: url(./zhang.jpg);">//这里可以更改成自己喜欢的相片
<div class="index_01"> 
<table style="width: 100%;height:100%;" >
  <tr>
    <td align="center" >
      <table align="center" width=350 height=230; class="index_table" >  
       <form method ="POST" action = "doloadling.php" name="frmLogin"> 
	   <tr align="center" style="font-size:25px;"> 
           <td colspan="2" style="font-size:35px;">zhang的留言板</td>
     </tr>
     <tr align="center" style="font-size:25px;"> 
           <td colspan="2" style="font-size:35px;">用户登陆</td>
     </tr>
       <tr>  
           <td align="center" style="font-size:25px;">用户名</td>  
           <td><input type="name" maxlength="16" name="uid" placeholder="请输入账号" style="width:180px;font-size:20px;border-radius: 8px; "></td>  
       </tr>  
       <tr>  
           <td align="center" style="font-size:25px;">密   码</td>  
           <td><input name="password" type="password" maxlength="16" placeholder="请输入密码" style="width:180px;font-size:20px;border-radius: 8px; "></td>
       </tr>
       <tr align="center"> 
           <td colspan="2">
           <input type="submit" name="denglu" value="登陆" class="btn">
           <input type="reset" name="rs" value="重置" class="btn">  
           <input type="button" name="zu" value="注册" onclick="window.location.href='register.php'" class="btn"/>  
           </td>  
       </tr> 
     </form>
     </table>
    </td>
  </tr>
</table> 
</div>
</body>
</html> 

index1.css

.btn {
    color: white;
    background-color: #2579d1;
    padding: 0px 1em;
    height: 35px;
	font-size:17px;
	border-radius: 12px;
   /* border: 0px solid black;*/
}
 
.btn:hover {
    background-color: #00ff00;
}
.index_01{
   height: 400px;width: 400px;
   margin:auto; 
   position: absolute;top: 0;
   left: 0;right: 0;bottom: 0;
   border-radius: 50%; 
   background-color: rgba(50,150,550,0.5);
   font-family:宋体;
}
div.k1 {
	margin-top:55px;
    margin-left:auto;
    margin-right:auto;
    max-width: 500px;
    background: #D2E9FF;
    padding: 20px 20px 20px 20px;
    color: #666;
}
h1 {
    font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
    padding: 10px 10px 10px 20px;
    display: block;
    background: #C0E1FF;
    border-bottom: 1px solid #B8DDFF;
    margin: -20px -20px 15px;
    }
h1>span {
    display: block;
    font-size: 14px;
}
label {
    display: block;
    margin: 0px 0px 5px;
}
label>span {
    float: left;
    width: 22%;
    text-align: right;
    padding-right: 15px;
    margin-top: 10px;
    font-weight: bold;
}
input[type="text"],textarea,select {
    color: #888;
    width: 70%;
    padding: 0px 0px 0px 5px;
    border: 1px solid #C5E2FF;
    background: #FBFBFB;
    outline: 0;
    -webkit-box-shadow:inset 0px 1px 6px #ECF3F5;
    box-shadow: inset 0px 1px 6px #ECF3F5;
    font: 200 12px/25px "Trebuchet MS", Arial, Helvetica, sans-serif;
    height: 30px;
    line-height:15px;
    margin: 2px 6px 16px 0px;
}
textarea{
    height:100px;
    padding: 5px 0px 0px 5px;
    width: 70%;
}
.button,.reset,.submit,.button1{
    padding: 10px 30px 10px 30px;
    background: #66C1E4;
    border: none;
    color: #FFF;
    box-shadow: 1px 1px 1px #4C6E91;
    -webkit-box-shadow: 1px 1px 1px #4C6E91;
    -moz-box-shadow: 1px 1px 1px #4C6E91;
    text-shadow: 1px 1px 1px #5079A3;
}
.button:hover,.reset:hover,.submit:hover,.button1:hover{
    background: #9AFF02;
}
.wrapper {
    width: 600px;
    margin: 0 auto;
}
.ds-post-main {
    position: relative;
    width: 500px;
    margin-left: auto;
    margin-right: auto;
}
.ds-comment-body {
    z-index: 1;
    position: relative;
    left: 0;
    background: #F0F0E3;
    padding: 15px 15px 15px 30px;
    color: #696A52;
    border-radius: 5px;
    box-shadow: 0 1px 2px rgba(0,0,0,.15), 0 1px 0 rgba(255,255,255,.75) inset;
}
.ds-comment-body {
    color: #696A52;
}
.ds-avatar {
    z-index: 2;
    position: absolute;
    top: 48px;
    left: -20px;
    padding: 5px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    box-shadow: -1px 0 1px rgba(0,0,0,.15) inset;
}
.ds-avatar a {
    display: block;
    padding: 1px;
    width: 32px;
    height: 32px;
    border: 1px solid #B9BAA6;
    border-radius: 50%;
    background-color: #fff;
    -moz-transition: color 0.15s linear;
    -webkit-transition: color 0.15s linear;
    transition: color 0.15s linear;
}
a {
    text-decoration: none;
}

 register.php

<html>/**
这里是注册页面
小zhangQQ:205187370**/
<head>
<title>注册</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./css/index1.css">
<style>
body{height:100%;}
</style>
</head>
<body style="background: url(./zhang.jpg)">
<div class="index_01" > 
<table style="width: 100%;height:100%;" >
  <tr>
    <td align="center" >
      <form action="doregister.php " name="dl" method="post">
      <table  align="center" width=350 height=230; style="font-family:宋体;font-size:25px;">
      <tr align="center"> 
          <td colspan="2" style="font-size:35px;">注册用户</td>
      </tr>
      <tr>
          <td align="center">用户名</td>
          <td>
          <input type="name" maxlength="20" name="id" placeholder="手机号/邮箱" style="width:180px;font-size:20px;border-radius: 8px; ">
          </td>
      </tr>
      <tr>
          <td align="center">密   码</td>
          <td >
          <input name="password" type="password" maxlength="16" placeholder="请输入密码" style="width:180px;font-size:20px;border-radius: 8px; ">
      </td>
      </tr>
      <tr>
          <td align="center">Again</td>
          <td>
          <input name="confirmPassword" type="password" maxlength="16" placeholder="请再次输入密码" style="width:180px;font-size:20px;border-radius: 8px; ">
          </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
        <input type="button" name='zu' value='登陆' onclick="location.href='index.php'" style="font-size:17px;border-radius: 12px;" class="btn"/>
        <input type="reset" name="zu" value="重置" style="font-size:17px;border-radius: 12px;" class="btn"> 
        <input type="submit" name="zu" value="注册" style="font-size:17px;border-radius:12px;" class="btn"/>
        </td>
      </tr>
   </table>
   </form>
    </td>
  </tr>
</table>
</div>
</body>
<html>

doloadling.php

<?php
require_once "connet.php";
require_once 'mysqlconfig.php';
$id=$_POST['uid'];
session_start();
$_SESSION["uid"]=$id;
$password=$_POST['password'];
$ma1=new DB();
   $link=$ma1->connect();
   $sql = "select * from tbl_ms where username='$id' and password='$password'";  
   $res = $ma1->CheckUser($link,$sql);
   if($res){
        header("Location:add.php");
   };
   if(!$res){
        echo "<script>alert('登录失败,账号或密码错误');location='index.php';</script>";
   };
?>

doregister.php

<?php
require_once "connet.php";
require_once 'mysqlconfig.php';
$ma1=new DB();
$link=$ma1->connect();
$id=$_POST['id'];
$password=$_POST['password'];
$confirmPassword=$_POST['confirmPassword'];
if($password!=$confirmPassword){
	echo "<script>alert('输入的密码和确认的密码不相等');location='register.php';</script>";;
}
$alt="select * from tbl_ms where username='$id'";
$res = $ma1->print1($link,$alt);
if($id!=null&&$password!=null){
   $ma=new DB();
   $link=$ma->connect();
   $sql = "insert into tbl_ms (username,password) values('$id','$password')"; 
   for ($i=0; $i < count($res); $i++) {
	   if($id!=$res[$i]['username']){
	   $res = $ma->insert($link,$sql);
	   };
       if($id==$res[$i]['username']){
           echo "<script>alert('注册失败,该账号已被注册!');location='register.php';</script>";
       }
   }
}
else{
    echo "<script>alert('注册失败,请输入账号和密码');location='register.php';</script>";
}
?>

add.php

<?php
/**
这里是提交留言页面
小zhangQQ:205187370**/
    header('Content-type: text/html; charset=UTF8');
    ?>
	<html>  
    <head>
	<link rel="stylesheet" type="text/css" href="./css/index1.css">
    <title>我的留言板.添加留言</title>  
    </head> 
	<body style="background: url(./zhang.jpg);">
	<div class="index_01"> 
	<table style="width: 100%;height:100%;" >
    <body background="./images/7.jpg" style="background-size:cover;" >  
    <center>  
    <h2>我的留言板</h2> 
    <input type = "button" value = "添加留言" onclick="location.href='add.php'" class="button"/>
    <input type = "button" value = "查看留言" onclick="location.href='show.php'" class="button"/>
	<input type = "button" value = "退出登陆" onclick="location.href='index.php'" class="button"/>
    <hr width = "70%"> 
    </center>
	<div class="k1">
    <form action = "doAdd.php" method = "post">  
    <h1>Add A Message
    <span>What's New To Share With You。</span>
    </h1>
    <label>
    <span>Your Name :</span>
    <input type="text" name="author" placeholder="Your Full Name" />
    </label>
    <label>
    <span>Title :</span>
    <input type="text" name="title" placeholder="Please input title" />
    </label>
    <label>
    <span>Message :</span>
    <textarea name="content" placeholder="Your Message to Us"></textarea>
    </label>
    <div style="margin-left:125px">
    <input type="submit" value="提交" class="submit">
    <input type = "reset" value = "重置" class="reset">
    </div>
	</div>
    </form>
    </body>  
    </html>  

doAdd.php

    
    <?php
	session_start();
    $id=$_SESSION["uid"];
    $title = $_POST["title"];  
    $author = $_POST["author"];
	$_SESSION["authorl"]=$author;
    $content = $_POST["content"];  
    $ip = $_SERVER["REMOTE_ADDR"];
	require_once "connet.php";
    require_once 'mysqlconfig.php';
    $ma1=new DB();
    $link=$ma1->connect();
    $sql = "insert into tbl_ms1 (user,title,author,ip,liuyan,time) values('$id','$title','$author','$ip','$content',now())";
    if($title!=null){
		if($author!=null){
		    $res = $ma1->insertl($link,$sql);
		};
	    if($author==null){
		    echo "<script>alert('请输入留言者!');location='add.php';</script>";
		};
	};
	if($title==null){
	    echo "<script>alert('请输入留言标题!');location='add.php';</script>";
	};
    ?>

show.php

    <?php
    header('Content-type: text/html; charset=UTF8');
    ?>
	<html>  /**
这里是查看留言页面
小zhangQQ:205187370**/
    <head>  
    <title>我的留言板.查看留言</title> 
	<link rel="stylesheet" type="text/css" href="./css/index1.css">
    </head>  
	<body style="background: url(./zhang.jpg);">
	<div class="index_01"> 
	<table style="width: 100%;height:100%;" >
    <body background="./images/7.jpg" style="background-size:cover;background-attachment: fixed;" >  
    <center>  
     <h2>我的留言板</h2>  
    <input type = "button" value = "添加留言" onclick="location.href='add.php'" class="button"/>
    <input type = "button" value = "查看留言" onclick="location.href='show.php'" class="button"/>
	<input type = "button" value = "退出登陆" onclick="location.href='index.php';logout()" class="button"/>
    <hr width = "70%"> 
	</center>
       <?php  
        //数据库连接  
        $con = @mysqli_connect("127.0.0.1","root","","zhang");  //这里也要改成自己的数据库
        if(!$con){  
            die("数据库连接错误".mysqli_connect_error());  
        }  
        mysqli_query($con,"set names 'utf8'");  
        //显示每页的留言数  
        $pagesize = 8;  
        //确定页数p参数  
        @$p = $_GET['p']?$_GET['p']:1;  
        //数据指针  
        $offset = ($p-1)*$pagesize;  
        //查询本页现实的数据 
		session_start();
        $id=$_SESSION["uid"];
        $query_sql = "select * from tbl_ms1 where user= '$id' order by user desc limit $offset,$pagesize";  
        # echo $query_sql;  
        $result = mysqli_query($con,$query_sql);  
		/*if (!$result) {
			printf("Error: %s\n", mysqli_error($con));
			exit();
		}*/
        //循环输出  
		echo "<div style='margin-top:55px'>";
		while($res = mysqli_fetch_array($result)){
			echo "<div class='k'>";
			echo "<div class='ds-post-main'>";
			echo "<div class='ds-comment-body'>
			<span>{$res['author']}  于  {$res['time']}  给我留言</span>
			<span style='float:right'><a href = 'del.php?id=".$res['id']."'><input type='submit' class='button1' value='删除'></input></a></span>
			<p>留言主题 : {$res['title']}   留言地址 : <span>{$res['ip']}</span></p>
            <hr width=450px> 
			<p>{$res['liuyan']}</p></div><br>";
			echo "</div>";
			echo "</div>";
        }
		echo "</div>";
        //分页代码  
        //计算留言总数  
        $count_result = mysqli_query($con,"select count(*) as count from tbl_ms1 where user= '$id'");  
        $count_array = mysqli_fetch_array($count_result);  
      
        //计算总的页数  
        $pagenum = ceil($count_array['count']/$pagesize);  
        //echo '共',$count_array['count'],'条留言';  
		//echo '共',$pagenum,'页';
        echo "<center>";
        echo "<div style='display: inline-block;margin-right: 15px;margin-left:15px;'>",'共',$count_array['count'],'条留言','</div>';  
		echo "<div style='display: inline-block;margin-right: 15px;margin-left:15px;'>",'共',$pagenum,'页','</div>';  
      
        //循环输出个页数及链接  
        if($pagenum>1){  
            for($i = 1;$i<=$pagenum;$i++){  
                if($i == $p){  
                    echo "<div style='background:#e8ffc4;width:25px;display: inline-block;margin-right: 10px;'>",$i,"</div>";
                      
                }else{  
                    //echo  '<a href="show.php?p=',$i,'">',$i,'</a>';
					echo '<a href="show.php?p=',$i,'">',"<div style='width:25px;display: inline-block;margin-right: 10px;background:#FF9D6F'>",$i,'</div>','</a>';
                }
            }
	echo "<div style='display: inline-block;margin-right: 10px;'>",'当前在 ',$p, ' 页',"</center></div>";
        }
		echo "<br>";
		echo "<br>";
		echo "<br>";
		echo "</div>";
    ?>
	<script type="text/javascript"> 
      function logout(){ 
        <% 
          session.invalidate(); //运用invalidate()比较好,退出时使session失效
        %> 
      }
    </script>
    </body>  
    </html> 

del.php

    <?php
    header("content-type:text/html;charset=utf-8");
	session_start();
    require_once "connet.php";
    require_once 'mysqlconfig.php';
    $ma1=new DB();
    $link=$ma1->connect();
	$id = $_GET['id'];
	//session_start();
    //$id=$_SESSION["uid"];
    if($link){
       $sql="delete from tbl_ms1 where id =$id ";
	   //echo "$sql";
       $que=mysqli_query($link,$sql);
		if($que){
		   echo "<script>alert('删除成功,返回首页');location='show.php';</script>";
		}else{
		   echo "<script>alert('删除失败');location='show.php'</script>";
		   exit;
		}
      }
    ?>

connet.php

<?php  
/**
这里是数据库连接
小zhangQQ:205187370**/
    define('DB_HOST', 'localhost');//地址IP  
    define('DB_USER', 'root'); // 数据库用户
    define('DB_PWD', '');  //数据库密码
    define('DB_CHARSET', 'UTF8');  
    define('DB_DBNAME', 'zhang');  //数据库表
?>

mysqlconfig.php

<?php
   class DB{
   	function connect(){
           @$link = mysqli_connect(DB_HOST,DB_USER,DB_PWD);//连接数据库
           mysqli_set_charset($link,DB_CHARSET);//设置数据库字体格式
           mysqli_select_db($link,DB_DBNAME) or die('数据库打开失败');//选择数据库
          if(mysqli_connect_errno())
        {
        die('数据库连接失败 : '.mysqli_connect_errno());
        }
        return $link;
    }
    function insert($link,$sql){
      if (mysqli_query($link, $sql)) { 
          echo "<script language='javascript'> alert('注册成功!');location='index.php'; </script>"; 
      } else { 
          echo "Error insert data: " . $link->error; 
      }
 
     }
    function CheckUser($link,$sql){
      $result = mysqli_query($link,$sql);
      $row = mysqli_num_rows($result);
      if($row != 0){
        return true;
        }
	  else{
        return false;
        }
     }
    function insertl($link,$sql){
       if (mysqli_query($link, $sql)) { 
          echo "<script language='javascript'> alert('留言成功!');location='show.php'; </script>"; 
       } else { 
          echo "Error insert data: " . $link->error; 
      }
 
     }
	 function print1($link,$sql){
        $result = mysqli_query($link,$sql);
        $data = array();
        while ($row = mysqli_fetch_array($result)) {
          $data[] = $row;
        }
        if ($data) {
          return $data;
        } else {
          return false;
        }
      }
     
}
?>

三、运行结果

 ok,这里还有数据库设计就不细列了!

这篇关于PHP+Wampserver(其中的MariaDB数据库)制作留言板的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!