创建数据表与认识PDO学习
pdo\config\database.php
<?php /** * 返回一个数组 数据库连接配置 * pdo扩展为PHP访问数据库定义了一个轻量级的一致接口 * pdo php data object 为php提供了数据访问抽象层 不管使用哪种数据库服务,都可以使用相同的函数(方法)去查询数据 * */ // phpinfo(); namespace pdo_chloe; return [ 'type'=> $type ?? 'mysql',//数据库类型 'host'=> $host ?? 'localhost',//数据库主机名 'dbname'=> $dbname ?? '16',//数据库名 'username'=> $username ?? 'root',//数据库连接用户名 'password'=> $password ?? '3.1415926',//密码 'charset'=> $charset ?? 'utf8mb4',//字符集 'port'=> $port ?? '3306'//端口 ]; ?>
pdo\1-connect.php <?php // echo password_hash('KILAKA',PASSWORD_BCRYPT); // ECHO time(); namespace connect_chloe; use PDO; $config = require __DIR__ .'\\config\\database.php'; extract($config); // var_dump($username); // dsn data source name 包括pDO驱动名称 主机名 端口号 数据库名称 $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()); } ?>
pdo\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(){ 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','disabled'); }else{ $("#err_msg").text(res.msg); $("#err_msg").css('color','green'); $('input[name="password"]').removeAttr('disabled'); } },'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>
pdo\3-doSubmit.php <?php namespace pdo_chloe; require 'common.php'; // 接收数据 // var_dump($_POST); $username = !empty($_POST['username']) && isset($_POST['username']) ? $_POST['username']:null; $password = !empty($_POST['password']) && isset($_POST['password']) ? password_hash($_POST['password'],PASSWORD_BCRYPT):null; $gender = !empty($_POST['gender']) && isset($_POST['gender']) ? intval($_POST['gender']):null; $c_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: $sql = "INSERT INTO `user` SET `uname` = '{$username}', `pwd`='{$password}', `gender` = {$gender},`c_time`={$c_time};"; // 执行 insert sql语句 // query()select sql 语句 // var_dump(empty($username)); if( empty($username) || empty($password) || empty($gender)) { echo json_encode(['status'=>10,'msg'=>'系统字段错误']); }else{ $flag = checkUname($username); if(!$flag) { $res = $pdo->exec($sql); // 数据插入成功 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: # code... break; } ?>
pdo\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() { } ?>