MySQL数据库
创建新数据库步骤
1.进入本地服务器:localhost:80端口
2.打开数据库:phpmyadmin
3.新建数据表:utf8_general_ci【多语言不区分大小写】
4.配制数据表信息
CREATE TABLE `h5_1906`.`users` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '用户id' ,
`user_name` VARCHAR(20) NOT NULL COMMENT '用户名' ,
`pass_word` VARCHAR(20) NOT NULL COMMENT '密码' ,
`user_email` VARCHAR(20) NOT NULL COMMENT '邮箱' ,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
名字 | 类型 | 索引 | A_I | 注释 |
---|---|---|---|---|
id | int | PRIMARI | √ | 用户ID |
user_name | VARCHAR | / | / | 用户名 |
user_password | VARCHAR | / | / | 密码 |
user_email | VARCHAR | / | / | 邮箱 |
5.数据库操作
【INSERT】增:
INSERT INTO `user`(`id`, `user_name`, `user_password`, `user_email`) VALUES ([value-1],[value-2],[value-3],[value-4])
【SELECT】查:
SELECT `id`, `user_name`, `user_password`, `user_email` FROM `user` WHERE 1
【UPDATE】改:
UPDATE `user` SET `id`=[value-1],`user_name`=[value-2],`user_password`=[value-3],`user_email`=[value-4] WHERE 1
【DELETE】删:
DELETE FROM `user` WHERE 1
6.连接数据库
?php
header('content-type:text/html;charset=utf-8');
$mysql_conf = array(
'host'=>'localhost:3306',
'db_user'=>'账号',
'db_pwd'=>'密码',
'db'=>'数据库名'
);
$mysqli = @new mysqli($mysql_conf['host'],$mysql_conf['db_user'],$mysql_conf['db_pwd']);
if($mysqli->connect_errno){
die('连接错误'.$mysqli->connect_errno);
}
$mysqli->query("set name 'utf8';");
$select_db = $mysqli->select_db($mysql_conf['db']);
if(!$select_db){
die('数据库连接错误'.$mysqli->error);
}
?>
7.注册业务
//注册的业务逻辑
// 1. 连接数据库
// 2. 接收前端传过来的数据
// 3. 在数据库中查询 用户名是否存在
// 4. 存在 注册失败 不存在 将数据写入数据库 注册成功
<body>
<form action="reg.php" method="POST"> //连接php文件
<label for="username">用户名</label>
<input type="text" id="username" name="username"><br>
<label for="password">密码</label>
<input type="password" id="password" name="password"><br>
<input type="submit">
</form>
</body>
<?php
include('./connect.php'); //引入数据库连接文件
$username = $_REQUEST['username']; //获取用户名
$password = $_REQUEST['password']; //获取密码
$email = $_REQUEST['email']; //获取邮箱
$sql = "select * from users where user_name='$username'"; //搜索用户名
$result = $mysqli->query($sql);
if($result->num_rows>0){
echo '用户名已存在';
die;
}
$sql_ins = "insert into users(user_name,pass_word,user_email)values('$username','$password','$email')";
$res = $mysqli->query($sql_ins);
if($res){
// echo '注册成功';
echo "<script>alert('注册成功');location.href='eg11.login.html';</script>";
}
// echo $sql_ins;
?>