Correction status:qualified
Teacher's comments:写代码的时候, 数据类型一定要关注
一、创建数据库(01.php)
<?php //连接数据库参数 $servername = 'localhost'; $username = 'root'; $password = 'root'; //链接数据库 try{ $conn = new PDO("mysql:host=$servername",$username,$password); }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } //创建数据库 try{ //构造sql语句 $sql = "create database cinema"; //使用exec(),exec()执行成功后将返回受影响的行数 $conn->exec($sql); echo "数据库创建成功"; }catch(PDOException $e){ echo $sql . "<br />" . $e->getMessage(); } $conn = null; ?>
点击 "运行实例" 按钮查看在线实例
二、创建数据表(02.php)
<?php //创建数据表 $db = [ 'type' => 'mysql', 'servername' => 'localhost', 'dbname' => 'cinema', 'username' => 'root', 'password' => 'root' ]; //配置数据信息 $conn = "{$db['type']}:host={$db['servername']};dbname={$db['dbname']}"; //链接数据库 try{ $pdo = new PDO($conn, $db['username'], $db['password']); }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } //创建分类表 try{ $sql = "CREATE TABLE `category` ( `cate_id` int(3) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(30) NOT NULL COMMENT '栏目名称', `alias` varchar(30) NOT NULL COMMENT '栏目别名', PRIMARY KEY (`cate_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "; //使用exec()方法 $pdo->exec($sql); echo "分类表category创建成功<br />"; }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } //创建电影表 try{ //构建sql语句 $sql = "CREATE TABLE `movies` ( `mov_id` int(2) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(100) NOT NULL COMMENT '影视剧名', `image` varchar(100) NOT NULL, `detail` text NOT NULL, `cate_id` int(3) unsigned NOT NULL COMMENT '所属栏目', PRIMARY KEY (`mov_id`), KEY `cate_id` (`cate_id`), CONSTRAINT `movies_ibfk_1` FOREIGN KEY (`cate_id`) REFERENCES `category` (`cate_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "; //使用exec()方法 $pdo->exec($sql); echo "数据表movies创建成功<br />"; }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } //创建系统表 try{ $sql = "CREATE TABLE `system` ( `sys_id` int(1) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `title` varchar(50) NOT NULL COMMENT '页面标题', `desc` varchar(200) NOT NULL COMMENT '页面描述', `key` varchar(50) NOT NULL COMMENT '关键字', `copy` varchar(50) NOT NULL COMMENT '版权', PRIMARY KEY (`sys_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; "; //使用exec()方法 $pdo->exec($sql); echo "系统表system创建成功<br />"; }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } //销毁 //$pdo = null; ?>
点击 "运行实例" 按钮查看在线实例
三、尝试预处理插入单条数据(03.php)
<?php //插入数据 预处理 //连接数据库文件 require_once __DIR__ . '/connect.php'; //创建SQL预处理插入模板 $sql = 'insert into `system` set `sys_id`=:sys_id, `title`=:title, `desc`=:desc, `key`=:key, `copy`=:copy'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //将变量绑定到SQL预处理模板的命名占位符上 $sys_id = 1; $title = '私人影院'; $desc = '收集全网最新最全的影视资源'; $key = '国产,欧美,日韩'; $copy = '梁伟杰'; $stmt->bindParam('sys_id', $sys_id, PDO::PARAM_INT); $stmt->bindParam('title', $title, PDO::PARAM_STR); $stmt->bindParam('desc', $desc, PDO::PARAM_STR); $stmt->bindParam('key', $key, PDO::PARAM_STR); $stmt->bindParam('copy', $copy, PDO::PARAM_STR); //执行SQL操作 if($stmt->execute()){ if($stmt->rowCount() > 0){ echo '成功添加' . $stmt->rowCount() . '条记录,该记录的主键ID是:' . $pdo->lastInsertId(); }else{ die('<pre>' . print_r($stmt->errorInfo(), true)); } } //销毁 $pdo=null; ?>
点击 "运行实例" 按钮查看在线实例
四、创建pdo链接数据库文件(connect.php)
<?php //创建数据表 $db = [ 'type' => 'mysql', 'servername' => 'localhost', 'dbname' => 'cinema', 'username' => 'root', 'password' => 'root' ]; //配置数据信息 $conn = "{$db['type']}:host={$db['servername']};dbname={$db['dbname']}"; //链接数据库 try{ $pdo = new PDO($conn, $db['username'], $db['password']); }catch(PDOException $e){ die('Connection Failed:' . $e->getMessage()); } ?>
点击 "运行实例" 按钮查看在线实例
五、修改hearder.php文件,读取数据库
删除相关数组,增加链接数据库代码
<?php require_once __DIR__ . '/connect.php'; //构建查询分类语句 $sql_c = "SELECT * FROM `category`"; $stmt_c = $pdo->prepare($sql_c); if($stmt_c->execute()){ $cates = $stmt_c->fetchAll(); } //构建查询电影语句 $sql_m = "SELECT * FROM `movies`"; $stmt_m = $pdo->prepare($sql_m); if($stmt_m->execute()){ $movies = $stmt_m->fetchALL(); } //构建底部版权语句 //使用desc倒叙排列,防止修改增加信息,只取最新一条 $sql_b = "SSELECT * FROM `system` order by `sys_id` desc limit 1"; $stmt_b = $pdo->prepare($sql_b); if($stmt_b->execute()){ $banquan = $stmt_b->fetchAll(); }
点击 "运行实例" 按钮查看在线实例
六、修改footer.php
<!--底部版权--> <div class="footer"> <p class="copyright"> <?php foreach($banquan as $bq) :?> <?php echo $bq['copy']?> © 版权所有</p> <?php endforeach; ?> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例
七、注意修改list.php detail.php文件
如:if ($cate['cate_id'] == $cate_id) {}
因为数据类型不一样,如果全等于,会导致无法读取数据