十几行代码实现网站注册系统(带数据库验证)

因爱好,需要一个数据库注册系统.然后硬憋了两天自己做了一个.

第一次接触php,真是受到刺激了.还有那么难用的编程语言,莫名奇妙的语法,莫名奇妙的刀了符.

下面是本人经历的错误历程

        1.index.html没法调用.php的文件,必须把index.php的后缀名换成.php才能在表中正常发送post请求(这是在本地用vscode的错误).

        2.服务器只能打开phpstudy自带的网站,我换成别的首页,始终打不开,www根目录也不行.到现在也没解决.只有修改phpstudy自带的index文件

        3.index表中antion属性只能调用系统自带的phpinfo文件,我自己写的都无法调用.也没有解决.

        4.phpstudy运行就会把index和phpinfo两个文件的后缀名去掉了.

        5.我下了宝塔面板,登录面板以后程序无法下载.

综上所述,phpstudy只认它自己带的两个网站文件.index与phpinfo.

下面是实现部分

第一步

        去官网下载一个phpstudy.放到你的服务器.我用的是2018

        小皮面板(phpstudy) - 让天下没有难配的服务器环境! (xp.cn)

第二步

        在服务器上设置好自己的数据库表

第三步

        打开网站根目录www文件夹

第四步

        替换index和phpinfo文件里面的内容

        index

        

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="php">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>卷云飞度</title>
    <style type="text/css">
        body {
            //这里是一张背景图片,放到www文件里
            background: url("./gucheng.jpg") no-repeat center center fixed;
            /*兼容浏览器版本*/
            -webkit-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
        }

        * {
            margin: 0;
            padding: 0
        }

        html,
        body {
            height: 100%
        }

        /*这里很关键*/

        .outer-wrap {
            /*只有同时为html和body设置height: 100%时,这里才生效,
            并且随浏览器窗口变化始终保持和浏览器视窗等高*/
            height: 100%;
            position: relative;

        }

        .login-panel {
            width: 400px;
            height: 400px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            font-family: 楷体;
        }

        input[type="submit"] {
            width: 20%;
            height: 40px;
            font-size: 20px;
            letter-spacing: 5px;
        }
    </style>
</head>

<body>

    <div class="outer-wrap">
        <div class="login-panel" style="text-align: center;">
            <form action="phpinfo.php" method="post">
                <div>
                    <label for="name" style="font-size: 24px;">用户名:</label>
                    <input type="text" id="name" name="name" placeholder="请输入账号" style="font-size: 20px;" required>
                </div>
                <br>
                <br>
                <div>
                    <label for="pwd" style="font-size: 24px;">密&nbsp;&nbsp;码: </label>
                    <input type="text" id="pwd" name="pwd" placeholder="请输入密码" style="font-size: 20px;" required>
                </div>

                <br>
                <br>
                <div>
                    <input type="submit" value="注册">
                </div>



            </form>
        </div>
    </div>
</body>

</html>

phpinfo

<?php
	// 连接到数据库
	$servername = "localhost";
	$username = "root"; //数据库账号
	$password = "root"; //密码
	$dbname = "test"; //数据库名
	
	$conn = new mysqli($servername, $username, $password, $dbname);
		if ($conn->connect_error) {
    			die("连接失败: " . $conn->connect_error);
			}
	$user = $_POST['name'];
	$pwd = $_POST['pwd'];

	//$username = mysqli_real_escape_string($conn, $_POST['username']);
	$sql = "SELECT * FROM 1000y WHERE name='$user'";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
    	echo "该用户名已经被占用。";
	return;
	}
	
	// 将用户信息插入数据库
$sql = "INSERT INTO 1000y (name, password) VALUES ('$user', '$pwd')";
if ($conn->query($sql) === TRUE) {

    //header("Location: index.php");
	    echo "注册成功!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
    

最后一步就是用你的服务器ip地址打开网站试试看.

最最后一步,愿世界没有php

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langliali

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值