下面这个时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!说明成功连接。
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>
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);
注意,变量在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 "账号注册失败,已存在账号~";
}
网页显示:
刷新下数据库:
OK,我们实现了网页数据保存到数据库了
1)虽然已经粗糙的完成,但是,还有一些问题,比如说当用户重复刷新“注册成功”页面后,数据库会有多条相同记录,因此我们需要设置个自增键以及需要令页面自动跳转,防止数据库出现BUG
下面是自增序列修复,实现了。
若需要自增插入,那就直接加个null,无需<>或’’
下面是页面自动跳转,我们假设跳转到百度
由此观之,确实跳转了,所以我们修复了一些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>