PHP实战(1)

来一次简单的实战吧

目标:

use your knowledge to realize the HTML(form info)-PHP(script)-MYSQL(database) connection(U can reference ready-made file source code).

思路:

HTML(form1:name,password)-PHP(combat1.php && mysql_database_connect.php)-MYSQL(server_test1.basic_info)

行动:

1.先写个脚本和数据表,连接数据库

下面这个是初始化mysql的表
在这里插入图片描述

下面这个时mysql_database_connect.php脚本,之后要被引用的

<?php
    header("Content-type:text/html,charset=utf-8");
    $connect_link=mysqli_connect("localhost:3306","root","sylym321");
    mysqli_query($connect_link,"set names utf8");
    mysqli_query($connect_link,"use server_test1");

下面这个是combat1.php,引用下刚刚的脚本
这里在调试的时候有个乱码的问题,我在修复时发现是header的顺序错了,header应该放在include之后,否则重复定义,相当于把字符集放入数据库后转一圈出来,变得乱七八糟了。因此我把header放在了include之后,变解决了这个问题。

<?php
	if(!include_once "mysql_database_connect.php"){
    
    //include initial file
    print_r("Failed to connect!");
    echo "<br>";
}
header('Content-type:text/html;charset=utf-8');//set browser charset utf-8

这时候我们的浏览器未显示Failed to connect!说明成功连接。

2.写个前端form表单

action 连我们的combat1.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FormStudy1</title>
</head>
<body>
        <div>
            <form method="post" action="../php/combat1.php">
                <span>注册账号</span> <input id="name" type="text" name="username"><br>
                <span>密码&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</span> <input id="ps" type="password" name="userpassword1"><br>
                <span>确认密码</span> <input id="ps1" type="password" name="userpassword2"><br>
                   <input type="radio" name="gender" value="1" checked="checked"><input type="radio" name="gender" value="2"><br>
                <button class="subline" @click="subline">提交</button>
                <br>
                <br>
            </form>
        </div>
</body>
</html>

这是网页的显示
在这里插入图片描述

3.将HTML页面的信息赋值给全局变量,进而再赋值给普通变量进行运算操作。

if($_POST['userpassword1']!=$_POST['userpassword2']){
    
    
    print_r('密码错误~');
    exit();
}
    $username=$_POST['username'];
    $userpassword=$_POST['userpassword1'];
    if($_POST['gender']==1) {
    
    
        $usersex = "男";
    }else if($_POST['gender']==2){
    
    
        $usersex = "女";
    }
    print_r($username);
    print_r($userpassword);
    print_r($usersex);

我们试着输出一下变量,发现没有乱码,说明到这部都OK。
在这里插入图片描述

4.然后就是通过sql指令将这些变量全部存储到数据库里了

注意,变量在SQL语句里的解析需要加上’'符号,因此这么写:

$mysql_insert_test_info = "insert into test_info values('$username','$userpassword','$usersex')";
if (mysqli_query($GLOBALS['connect_link'], $mysql_insert_test_info)) {
    
    
    echo "账号注册成功~";
} else {
    
    
    echo "账号注册失败,已存在账号~";
}

5.调试

在这里插入图片描述网页显示:
在这里插入图片描述
刷新下数据库:
在这里插入图片描述
OK,我们实现了网页数据保存到数据库了

6.BUG修复

1)虽然已经粗糙的完成,但是,还有一些问题,比如说当用户重复刷新“注册成功”页面后,数据库会有多条相同记录,因此我们需要设置个自增键以及需要令页面自动跳转,防止数据库出现BUG

下面是自增序列修复,实现了。
若需要自增插入,那就直接加个null,无需<>或’’
在这里插入图片描述

下面是页面自动跳转,我们假设跳转到百度
在这里插入图片描述在这里插入图片描述在这里插入图片描述
由此观之,确实跳转了,所以我们修复了一些BUG

源代码参考:
1.mysql_database_connect.php

<?php
    header("Content-type:text/html,charset=utf-8");
    $connect_link=mysqli_connect("localhost:3306","root","");
    mysqli_query($connect_link,"set names utf8");
    mysqli_query($connect_link,"use server_test1");

2.combat1.php

<?php
//target:use your knowledge to realize the HTML(form info)-PHP(script)-MYSQL(database) connection(U can reference ready-made file source code).
//thinking:HTML(form1:name,password)-PHP(combat1.php && mysql_database_connect.php)-MYSQL(server_test1.basic_info)
//first,connect MYSQL
if(!include_once "mysql_database_connect.php"){
    
    //include initial file
    print_r("Failed to connect!");
    echo "<br>";
}
header('Content-type:text/html;charset=utf-8');//set browser charset utf-8
//second,receive the html form info
if($_POST['userpassword1']!=$_POST['userpassword2']){
    
    
    print_r('密码错误~');
    exit();
}
    $username=$_POST['username'];
    $userpassword=$_POST['userpassword1'];
    if($_POST['gender']==1) {
    
    
        $usersex = "男";
    }else if($_POST['gender']==2){
    
    
        $usersex = "女";
    }
//third,Assign this variable to mysql function
$mysql_insert_test_info = "insert into test_info values(null,'$username','$userpassword','$usersex')";
if (mysqli_query($GLOBALS['connect_link'], $mysql_insert_test_info)) {
    
    
    echo "账号注册成功~";
    header("Location:http://www.baidu.com");
    exit();
} else {
    
    
    echo "账号注册失败,已存在账号~";
}

3.form1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FormStudy1</title>
</head>
<body>
        <div>
            <form method="post" action="../php/combat1.php">
                <span>注册账号</span> <input id="name" type="text" name="username"><br>
                <span>密码&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</span> <input id="ps" type="password" name="userpassword1"><br>
                <span>确认密码</span> <input id="ps1" type="password" name="userpassword2"><br>
                   <input type="radio" name="gender" value="1" checked="checked"><input type="radio" name="gender" value="2"><br>
                <button class="subline" @click="subline">提交</button>
                <br>
                <br>
            </form>
        </div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_52480906/article/details/120932845