2019-10-12,html+php+mysql簡單留言板,作業


php+mysql簡易留言板,實現注冊,登錄,注銷,查看留言,刪除留言

1,index.html登錄頁面

 

代碼:

<!doctype html>

<html>

    <head>

        <meta charset="UTF-8">

        <title>登陸</title>

    <style type="text/css">

        *{

            margin: 0;

            padding: 0;

           

        }

        div{

            width: 200px;

            height: 200px;

           

        }

        .center-in-center{

            position: absolute;

            top: 50%;

            left: 50%;

            -webkit-transform: translate(-50%, -50%);

            -moz-transform: translate(-50%, -50%);

            -ms-transform: translate(-50%, -50%);

            -o-transform: translate(-50%, -50%);

            transform: translate(-50%, -50%);

        }

    </style>

    </head>

    <body bgcolor="#d0d0d0">

        <div class="center-in-center">

        <h2 align="center">用戶登錄</h2>

           <form name="dl" action="dl.php" method="post">

                <p align="center">用戶名:<input type=text name="name"></p>

                <p align="center">密碼:<input type=password name="password"></p>

                <p align="center"><input type="submit" name="denglu" value="登錄"></p>

            </form>    

           

            <form name="ze" action="zc.html" method="post" >

            <p align='center'><input type="submit" name="zhuce" value="注冊" > </p>     

            </form> </div>

       

    </body>

</html>

 

css樣式用來將頁面標簽居中對齊

 

2,建立數據庫連接文件conn.php

<?php

    $server="localhost:3306";//主機的IP地址,你也可以選填127.0.0.1

    $db_username="root";//數據庫用戶名

    $db_password="root";//數據庫密碼

 

    $con = mysqli_connect($server,$db_username,$db_password);//鏈接數據庫

    if(!$con){

        die("can't connect".mysql_error());//如果鏈接失敗輸出錯誤

    }

   

    mysqli_select_db($con,'zhuce');//選擇數據庫

?>

 

3,登錄后端驗證dl.php

<?php

    header("Content-Type: text/html; charset=utf8");

    if(!isset($_POST["denglu"])){

        echo "錯誤執行";

        echo "<script>                       setTimeout(function(){window.location.href='index.html';},2000);                  </script>";}//檢測是否有submit操作

    include('conn.php');//鏈接數據庫

   

$name = $_POST['name'];//post獲得用戶名表單值

 

$passowrd = $_POST['password'];//post獲得用戶密碼單值

 

    if ($name && $passowrd){//如果用戶名和密碼都不為空

             $sql = "select * from t1 where username = '$name' and password='$passowrd'";//檢測數據庫是否有對應的username和password的sql

             $result = mysqli_query($con,$sql);//執行sql

             $rows=mysqli_num_rows($result);//返回一個數值

             if($rows){//0 false 1 true

             echo $rows;

             header("refresh:0;url=ly.html");//如果成功跳轉至welcome.html頁面

             exit;

             }

           

             else{

               

                echo "<script>alert('用戶名或密碼錯誤,點擊確定返回!');</script>";

               

                header("refresh:0;url=index.html");

             }

            

 

    }else{//如果用戶名或密碼有空

                echo "<script>alert('用戶名或不能為空,點擊確定返回!');</script>";

                header("refresh:0;url=index.html");

    }

 

    mysqli_close($con);//關閉數據庫

?>

4,注冊前端zc.html,

 

代碼:

<!DOCTYPE html>

<html>

<head>

    <title>注冊</title>

    <meta charset="utf-8">

        <style type="text/css">

        *{

            margin: 0;

            padding: 0;

           

        }

        div{

            width: 200px;

            height: 200px;

           

        }

        .center-in-center{

            position: absolute;

            top: 50%;

            left: 50%;

            -webkit-transform: translate(-50%, -50%);

            -moz-transform: translate(-50%, -50%);

            -ms-transform: translate(-50%, -50%);

            -o-transform: translate(-50%, -50%);

            transform: translate(-50%, -50%);

        }

    </style>

</head>

<body bgcolor="#d0d0d0">

       <div class="center-in-center">

       <h2 align="center">用戶注冊</h2>

   

        <form action="zhuce.php" method="post">

            <p align='center'>用戶賬戶:<input id="user" name="user" type="text" placeholder="用戶名"/></p>

            <p align='center'>注冊密碼:<input id="psd1" name="psd1" type="password" placeholder="密碼"/></p>

            <p align='center'>重復密碼:<input id="psd2" name="psd2" type="password" placeholder="驗證密碼"/></p>

            <p align='center'>注冊郵箱:<input id="eml" name="eml" type="email" placeholder="郵箱"/></p>

            <p align='center'><input id="sbt" name="sbt" type="submit" placeholder="提交"/></p>

        </form>

    </div>

</body>

</html>

 

5,注冊后端驗證zhuce.php

 

 

代碼:

<?php

    header("content-type:text/html;charset=utf-8");

    session_start();

    $name=$_POST['user'];

    $pwd=$_POST['psd1'];

    $repwd=$_POST['psd2'];

    $email=$_POST['eml'];

    if(empty($name)||empty($pwd)||empty($repwd)||empty($email)){

        echo "<script>alert('你逗我?信息輸入沒完整');</script>";

        echo "<script>window.location='zc.html';</script>";

    }else

 

    if ($pwd!=$repwd) {

        echo"<script>alert('兩次密碼輸入不一致,請重新輸入');</script>";

        echo"<script>location='zc.html'</script>";

    }else{

 

            include('conn.php');

            $sql1 = "SELECT * FROM t1 WHERE username='$name'";

            $result = mysqli_query($con,$sql1);

            $rows = mysqli_num_rows($result);

            if($rows>0) {

                echo "<script>alert('用戶名已經有人注冊了,重新注冊一個吧')</script>";

                echo "<script>window.location='zc.html'</script>";

            }

            else {

                echo "用戶名可用\n".'<br>';

                mysqli_query($con,"set names utf8");

                $sqlinsert="insert into t1(username,password,email) values('{$name}','{$pwd}','{$email}')";

                $result=mysqli_query($con,$sqlinsert);

                if(! $result )

                    {

                      die('Could not enter data: ' . mysqli_error());

                    }

                echo "恭喜你注冊成功".'<br>';

                echo '用戶名:'.$name.'<br>';

                echo '密碼:'.md5($pwd).'<br>';

                            echo '注冊郵箱:'.$email.'<br>';

                echo "正在返回登錄界面,請稍后~";

                header("refresh:2;url=index.html");              

                  }

          }

          mysqli_close($con);

?>

6,登錄之后留言界面,前端,ly.html

 

代碼:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>

php留言板

</title>

<style>

p,textarea{vertical-align:top;}

</style>

</head>

 

<body bgcolor="#d0d0d0">

       <h2 align="center">請留言</h2>

<form action="ly.php" method="post" align='center'>

<p align="center">姓名:<input type="text" name="username" /></p>

<p align="center">留言:<textarea cols="30" rows="5" name="comment" /></textarea></p>

<input type="submit" value="提交">

</form>

<form action="chakan.php" method="post" align='center'>

       <input type="submit" name="chakan" value="查看留言" >

</form>

<form action="index.html" method="post" align='center'>

       <input type="submit" name="zhuxiao" value="注銷" >

</form>

</body>

</html>

 

7,留言頁面后端驗證,ly.php

 

 

代碼:

<?php

header("Content-Type: text/html; charset=utf8");

$user = $_POST['username'];

$comment = $_POST['comment'];

print_r($_POST);

if(empty($user)||empty($comment)){

        echo "<script>alert('姓名或內容沒輸入,請輸入好后提交!');</script>";

        echo "<script>window.location='ly.html';</script>";

    }

    else{

              include('conn.php');

              $sql="insert into t2(username,comment) values('$user','$comment')";

              $db=mysqli_select_db($con,'zhuce');

              if($db){

              $result = mysqli_query($con,$sql);

              echo "<script>alert('留言成功');</script>";

              header("refresh:0;url=ly.html");

                            }

              else{

              echo "留言失敗!";

              echo mysqli_errno($con);

                     }

              }

mysqli_close($con);

?>

 

8,查看留言,chakan.php

 

代碼:

<?php

echo "<body bgcolor='#d0d0d0'>";

include('conn.php');

$sql="select * from t2";

$db=mysqli_select_db($con,'zhuce');

$result = mysqli_query($con,$sql);

$array=array();

echo "<h2 align='center'>所有留言</h2>";

echo '<table border="1" align="center"><td>用戶</td><td>給你的留言</td>';

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

{

    echo "<tr><td> {$row['username']}</td> ".

         "<td>{$row['comment']} </td> ".

         "</tr>";

}

echo '</table>';

mysqli_close($con);

echo '<p><form action="ly.html" method="post" accept-charset="utf-8" align="center"><td><input type="submit" name="fanhui" value="點擊返回"></form></p>';

echo '<p><form action="del.php" method="post" accept-charset="utf-8" align="center"><input type="submit" name="shanchu" value="刪除留言"></form></p>';

echo "</body>";

?>

9,刪除留言,del.php

 

<html>

<head>

       <title>刪除留言</title>

</head>

<body bgcolor="#d0d0d0">

       <h2 align="center">請找到要刪除的留言,點擊“刪除”</h2>

<?php

include('conn.php');

$sql = "select * from t2";

mysqli_select_db($con,'zhuce');

$result = mysqli_query($con,$sql);

echo '<table border="1" align="center"><tr><td>用戶</td><td>給你留言</td></tr>';

while($attr = mysqli_fetch_array($result))

{

echo "<tr>

<td>{$attr[1]}</td>    

<td>{$attr[1]}</td>

<td><a onclick=\"return confirm('確定刪除么')\" href='delete.php?code={$attr[0]}'>刪除</a></td>

</tr>";

}

echo "</table>";

echo '<p><form action="chakan.php" method="post" align="center">

       <input type="submit" name="fh" value="點擊返回">

       </form></p>';

 ?>

 </body>

</html>

 

10,確定刪除,delete.php

 

<?php

include('conn.php');

$code = $_GET["code"];

$sql = "delete from t2 where id = '{$code}'";

mysqli_select_db( $con, 'zhuce' );

$retval = mysqli_query( $con, $sql );

if(! $retval )

{

  die('數據刪除失敗: ' . mysqli_error($con));

}

echo "數據刪除成功,馬上返回!";

mysqli_close($con);

header("refresh:1;url=del.php");

?>

 

11,數據庫文件,zhuce.sql

/*

 Navicat Premium Data Transfer

 

 Source Server         : localhost_3306

 Source Server Type    : MySQL

 Source Server Version : 50726

 Source Host           : localhost:3306

 Source Schema         : zhuce

 

 Target Server Type    : MySQL

 Target Server Version : 50726

 File Encoding         : 65001

 

 Date: 12/10/2019 18:51:57

*/

 

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;

 

-- ----------------------------

-- Table structure for t1

-- ----------------------------

DROP TABLE IF EXISTS `t1`;

CREATE TABLE `t1`  (

  `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL

) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

 

-- ----------------------------

-- Records of t1

-- ----------------------------

INSERT INTO `t1` VALUES ('1111', '111', '1111@qq.com');

INSERT INTO `t1` VALUES ('11111', '111', '1111@qq.com');

INSERT INTO `t1` VALUES ('sym', 'zzz', 'zzz@qq.com');

INSERT INTO `t1` VALUES ('111', '111', '1111@qq.com');

INSERT INTO `t1` VALUES ('222', '222', '222@a.com');

INSERT INTO `t1` VALUES ('asd', 'asd', 'asd@123.com');

INSERT INTO `t1` VALUES ('sym945', 'zxc', 'zxc@qq.com');

INSERT INTO `t1` VALUES ('555555', '555', '555@qq.com');

INSERT INTO `t1` VALUES ('syms', 'sss', 'sss@qq.com');

INSERT INTO `t1` VALUES ('zxzx', 'zxzx', 'zx@qq.co');

 

-- ----------------------------

-- Table structure for t2

-- ----------------------------

DROP TABLE IF EXISTS `t2`;

CREATE TABLE `t2`  (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  `comment` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = MyISAM AUTO_INCREMENT = 21 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

 

-- ----------------------------

-- Records of t2

-- ----------------------------

INSERT INTO `t2` VALUES (19, '123', '123123');

INSERT INTO `t2` VALUES (20, '22333', '22333');

 

SET FOREIGN_KEY_CHECKS = 1;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM