PHP+MySQL實現留言板功能(三)完整版
阿新 • • 發佈:2018-11-25
由於前段時間過於忙碌,加上小編有重灌了電腦系統,一直沒有去復現這個功能,後面復現過程中,程式碼做了一些調整。今天我將完整版的程式碼和sql檔案程式碼整理出來,供各位學習參考。
1.資料庫SQL檔案
index.php
<html> <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(./images/timg7.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;">使用者登陸</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>
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>"; }; ?>
register.php
<html> <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(./images/timg7.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>
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
header('Content-type: text/html; charset=UTF8');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/index1.css">
<title>我的留言板.新增留言</title>
</head>
<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>
<head>
<title>我的留言板.檢視留言</title>
<link rel="stylesheet" type="text/css" href="./css/index1.css">
</head>
<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","1728aceAB7","my1");
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['user']."'><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
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PWD', '17Bdda7'); //我資料庫密碼
define('DB_CHARSET', 'UTF8');
define('DB_DBNAME', 'my1');
?>
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;
}
}
}
?>
sql檔案
/*
Navicat MySQL Data Transfer
Source Server : MySQL
Source Server Version : 50617
Source Host : localhost:3306
Source Database : my1
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2018-11-06 22:53:31
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tbl_ms
-- ----------------------------
DROP TABLE IF EXISTS `tbl_ms`;
CREATE TABLE `tbl_ms` (
`username` varchar(25) NOT NULL,
`password` varchar(25) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for tbl_ms1
-- ----------------------------
DROP TABLE IF EXISTS `tbl_ms1`;
CREATE TABLE `tbl_ms1` (
`user` varchar(25) NOT NULL,
`title` varchar(225) DEFAULT NULL,
`author` varchar(25) DEFAULT NULL,
`ip` varchar(25) DEFAULT NULL,
`liuyan` varchar(225) DEFAULT NULL,
`time` time DEFAULT NULL,
KEY `user` (`user`),
CONSTRAINT `tbl_ms1_ibfk_1` FOREIGN KEY (`user`) REFERENCES `tbl_ms` (`username`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
css檔案
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;
}
檔案結構(.bak是備份檔案,不用理會)