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{

            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{

            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 "用户名可用 ".'<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;

原文地址:https://www.cnblogs.com/sym945/p/11663456.html