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的表
PHP实战(1)_第1张图片

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


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

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


	if(!include_once "mysql_database_connect.php"){//include initial file
    print_r("Failed to connect!");
    echo "
"
; } 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>FormStudy1title>
head>
<body>
        <div>
            <form method="post" action="../php/combat1.php">
                <span>注册账号span> <input id="name" type="text" name="username"><br>
                <span>密码        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>

这是网页的显示
PHP实战(1)_第2张图片

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。
PHP实战(1)_第3张图片

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.调试

PHP实战(1)_第4张图片网页显示:
PHP实战(1)_第5张图片
刷新下数据库:
PHP实战(1)_第6张图片
OK,我们实现了网页数据保存到数据库了

6.BUG修复

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

下面是自增序列修复,实现了。
若需要自增插入,那就直接加个null,无需<>或’’
PHP实战(1)_第7张图片

下面是页面自动跳转,我们假设跳转到百度
PHP实战(1)_第8张图片PHP实战(1)_第9张图片PHP实战(1)_第10张图片
由此观之,确实跳转了,所以我们修复了一些BUG

源代码参考:
1.mysql_database_connect.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


//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 "
"
; } 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>FormStudy1title>
head>
<body>
        <div>
            <form method="post" action="../php/combat1.php">
                <span>注册账号span> <input id="name" type="text" name="username"><br>
                <span>密码        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>

你可能感兴趣的:(MySQL,PHP,1024程序员节,php,mysql,数据库)