mysql基础语句与用户注册
mysql基础语句与用户注册
作业标题:mysql基础 与 pdo作业
作业内容:1. 总结mysql 常用DDL, DML语言并实操; 2. 新建一张用户表; 3. 预习pdo预处理;
- 总结mysql 常用DDL, DML语言并实操;
DDL语句
数据库的创建、删除、显示、引入
创建数据库:
CREATE DATABASE [IF NOT EXISTS] 数据库名;
删除数据库:
DROP DATABASE [IF EXISTS] 数据库名;
显示所有已建立的数据库名称列表:
SHOW DATABASES;
引入数据库为当前数据库:
USE 数据库名;
创建表
语法格式如下:
CREATE TABLE [IF NOT EXISTS] 表名称
(
字段名1 列类型 [属性] [索引],
…
字段名n 列类型 [属性] [索引]
)[表类型] [表字符集];
eg:
create table if not exists users(
id int(10) unsigned not null auto_increment,
username varchar(50) not null,
userpass varchar(50) not null,
telno varchar(20) not null unique,
sex enum(‘男’,’女’) not null default ‘男’,
brithday date not null default ‘0000-00-00’,
primary key(id),
index users_username(username,userpass)
)
ENGINE=MyISAM default character set utf8 collate utf8_general_ci;
上述中设置表类型为MyISAM使用ENGINE而不是Type。最后一句的意思是:指定默认创建的表字符集(character set)为utf8,校对规则是utf8_general_ci。
表创建成功后,可以使用“show tables”命令查看,使用“describe 表名”或者“desc 表名”命令用于显示表的创建结构。
删除表
drop table [if exists] 表名;
修改表
1、为指定的数据表新增一个字段:
alter table 表名 add 字段名 <建表语句> [firstafter 列名]
语法解析:
[firstafter 列名]:在指定的列名之前或之后添加
eg:在users表的第一列之前添加name列
alter table users add name varchar(30) not null first;
在users表的userpass之后添加height列
alter table users add height double not null default ‘0.00’ after userpass;
2、修改字段的类型或名称:
可以使用change或者modify子句,如果列名相同,则change和modify的作用相同
alter table 表名 change/modify 列表<建表语句>
eg:
修改users表中telno字段,将类型改为int,并设置默认值
alter table users modify telno int unsigned default ‘0’;
如果要修改列名,使用change,change除了更改类型外还能更改列名,而modify不能实现修改列名这个功能。
eg:
修改users表的telno的类型,并将telno命名为phone
alter table users change telno phone int unsigned default ‘0’;
注意:使用change,即使不更改列名,也需要在定义中包括相应的列名,即上述的“telno phone”需要写作“telno telno”。
3、重命名数据表
alter table 旧表名 rename 新表名;
4、删除表字段
alter table 表名 drop 列名;
DML语句
插入记录
1、插入单条记录:
insert into tablename(column1,column2…columnN) values(v1,v2…vN);
2、一次性插入多条记录:
insert into tablename(column1,column2,……columnN)
values
(r1_v1,r1_v2,……r1_vN),
(r2_v1,r2_v2,……r2_vN);
查询数据
1、使用LIMIT关键字,查询指定部分的数据,语法:
select ….from tablename [where..] [order by ..] limit startindex,rowcount
startindex表示起始偏移量,默认为0;rowcount表示查询的行数。
2、聚合查询
语法:
select [col1,col2,…coln] fun_name
from tablename
[where 条件语句]
[group by col1,col2,..coln
[with rollup]]
[having 分组后的条件语句]
- 新建一张用户表;
登录页面2-reg.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册</title>
</head>
<body>
<h2>用户注册</h2>
<table>
<tr>
<td>账户</td>
<td><input type="text" name="username"></td>
<td><span id="err_msg"></span></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="1" checked >男
<input type="radio" name="gender" value="0" >女
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="button" name="btn" value="确定">
</td>
</tr>
</table>
<script type="text/javascript" src="
https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" >
// 检测用户名是否重复
$('input[name="username"]').blur(function(){
//const data = new FormData();
var data = {};
data.username = $('input[name="username"]').val();
data.type = 1;
$.post('3-doSubmit.php',data,function(res){
if(res.status == 0)
{
$("#err_msg").text(res.msg);
$("#err_msg").css('color','red');
$('input[name="password"]').attr('disabled',true);
}else{
$("#err_msg").text(res.msg);
$("#err_msg").css('color','green');
$('input[name="password"]').removeAttr('disabled',true);
}
},'json')
})
// 检查用户名和密码不能为空
$('input[name="btn"]').click(function(){
var data = {};
data.username = $('input[name="username"]').val();
data.password = $('input[name="password"]').val();
data.gender = $('input[name="gender"]').val();
data.type = 2;
if(data.username == '' || data.password == '' )
{
alert('用户名或密码不能为空');
return;
}
$.post('3-doSubmit.php',data,function(res){
if(res.status == 1)
{
alert(res.msg);
}else{
alert(res.msg);
}
},'json')
})
</script>
</body>
</html>
数据库连接1-connect.php
<?php
namespace connect_chloe;
use PDO;
$config = require __DIR__.'\\config\\database.php';
extract($config);
//var_dump($type);
$dsn = sprintf('%s:host=%s;port=%d;dbname=%s',$type,$host,$port,$dbname);
try {
$pdo = new PDO($dsn,$username,$password,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING]);
// var_dump($pdo);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
} catch (\Exception $e) {
die('Connection error : ' . $e->getMessage());
}
接收数据分发处理3-doSubmit.php
<?php
namespace pdo_chloe;
//连接数据库
require '1-connect.php';
require 'common.php';
//接收数据
$username=!empty($_POST["username"]) && isset($_POST["username"])?$_POST["username"]:null;
$password=!empty($_POST["password"]) &&isset($_POST["password"])?$_POST["password"]:null;
$gender=!empty($_POST["gender"]) &&isset($_POST["gender"])?intval($_POST["gender"]):null;
$create_time=time();
//请求分发器type 1检查用户名重名 2注册 3登录
$type=isset($_POST['type'])&&!empty($_POST['type'])?intval($_POST['type']):null;
switch($type){
case 1:
$res=checkUname($username);
if($res)
{
echo json_encode(['status'=>0,'msg'=>'用户名已被占用'],320);
}else{
echo json_encode(['status'=>1,'msg'=>'用户名合法'],320);
}
break;
case 2:
if( empty($username) || empty($password) || empty($gender))
{
echo json_encode(['status'=>10,'msg'=>'系统字段错误']);
}else{
$flag = checkUname($username);
if(!$flag)
{
insertData($username,$password,$gender,$create_time);
// 数据插入成功
if($res)
{
echo json_encode(['status'=>1,'msg'=>'注册成功'],320);
}else{
echo json_encode(['status'=>0,'msg'=>'注册失败'],320);
}
}else{
echo json_encode(['status'=>0,'msg'=>'请勿重复提交'],320);
}
}
break;
default:
break;
}
公共函数common.php
<?php
//公共模型文件
require '1-connect.php';
function checkUname($username)
{
global $pdo;
$isOccupied=false;//默认数据库不存在同名用户名
$sql="SELECT `uname` FROM `user` WHERE `uname`='{$username}'";
$res=$pdo->query($sql)->fetch();
//已存在同名昵称
if($res)
{
$isOccupied=true;
}
return $isOccupied;
}
function insertData($username,$password,$gender,$create_time)
{
global $pdo,$res;
$sql="INSERT INTO `user` SET `uname`='{$username}',`pwd`='{$password}',
`gender`={$gender},`create_time`={$create_time};";
//执行 insert sql语句
$res=$pdo->exec($sql);
}
示例图
更多相关文章
- 补0817:域名空间内类实现自动加载,use的作用 及 mysql数据库DDL,D
- 补0818:数据库ddl,dml实操 及 新建用户表
- 通过链式调用,将一个类委托给另一个类实现数据库的查询操作
- MySql 常用 DDL - DML 语句
- 数据库连接及事件委托
- MySQL DDL命令大全(数据定义语言)
- mysql语句、类的自动加载与引用
- oracle数据库无法打开修复过程
- 补0816:拦截方法封装mysql查询语句