Correcting teacher:查无此人
Correction status:qualified
Teacher's comments:完成的不错,继续加油。
<?php //数据库连接参数 $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'zy', 'username' => 'root', 'password' => 'root', ]; //配置数据源DSN信息 //$pdo = new PDO('mysql:host=localhost;dbname=php','root','root'); $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try{ $pdo = new PDO($dsn,$db['username'],$db['password']); echo "连接成功!<br/>"; }catch(PDOException $e){ die( $e->getMessage() ); } ?>
点击 "运行实例" 按钮查看在线实例
<?php //链接数据库 require 'connect.php'; // 获取栏目信息 $sql = 'SELECT `cate_id`,`name` FROM `Shop`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $cates = $stmt->fetchAll(PDO::FETCH_ASSOC); $cate_count = count($cates); // 栏目数量 // 获取影视信息 $sql = 'SELECT `mov_id`,`mov_name`,`image`, `mov_js`, `cate_id` FROM `shop_varieties`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $movies = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>实践</title> <style type="text/css"> *{ padding: 0; margin: 0; } li{ list-style:none; } header,footer{ width: 100%; height: 60px; background: red; line-height: 60px; text-align: center; font-size: 18px; } main{ display: flex; align-items: center; background:#FFEB3B; } main>ul{ flex:1; background:pink; text-align: center; } main>h3{ flex:.2; text-align: center; } main>ul>li>img{ width: 150px; height: 120px; } main>ul>li{ padding: 5px; } </style> </head> <body> <header>商店</header>
点击 "运行实例" 按钮查看在线实例
<?php include __DIR__ . "/header.php"; ?> <main> <?php foreach ($cates as $cates_v) { // print_r($cates_v['name']); echo '<h3><a href="list.php?id='.$cates_v['cate_id'].'">'.$cates_v['name'].'</a></h3>'; echo '<ul>'; foreach ($movies as $movies_v) { // print_r($movies_v['mov_name']); if ($movies_v['cate_id'] == $cates_v['cate_id']) { echo '<li><a href="calist.php?id='.$movies_v['mov_id'].'&cate_id='.$movies_v['cate_id'].'">'.$movies_v['mov_name'].'</a></li>'; } } echo '</ul>'; } ?> </main> <?php include __DIR__ . "/footer.php"; ?>
点击 "运行实例" 按钮查看在线实例
<?php include __DIR__ . "/header.php"; ?> <main> <?php foreach ($cates as $cates_v) { // print_r($cates_v['name']); if ($cates_v['cate_id'] == $_GET['id']) { echo '<h3>'.$cates_v['name'].'</h3>'; echo '<ul>'; foreach ($movies as $movies_v) { // print_r($movies_v['mov_name']); if ($movies_v['cate_id'] == $cates_v['cate_id']) { echo '<li><a href="calist.php?id='.$movies_v['mov_id'].'&cate_id='.$movies_v['cate_id'].'">'.$movies_v['mov_name'].'</a></li>'; } } echo '</ul>'; } } ?> </main> <?php include __DIR__ . "/footer.php"; ?>
点击 "运行实例" 按钮查看在线实例
<?php include __DIR__ . "/header.php"; ?> <main> <?php foreach ($cates as $cates_v) { // print_r($cates_v['name']); if ($cates_v['cate_id'] == $_GET['cate_id']) { echo '<h3><a href="list.php?id='.$cates_v['cate_id'].'">'.$cates_v['name'].'</a></h3>'; echo '<ul>'; foreach ($movies as $movies_v) { // print_r($movies_v['mov_name']); if ($movies_v['mov_id'] == $_GET['id']) { echo '<li>'.$movies_v['mov_name'].'</li>'; echo '<li><img src="/img/'.$movies_v['image'].' "></li>'; echo '<li>'.$movies_v['mov_js'].'</li>'; } } echo '</ul>'; } } ?> </main> <?php include __DIR__ . "/footer.php"; ?>
点击 "运行实例" 按钮查看在线实例
<?php //数据库连接参数 $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'zy', 'username' => 'root', 'password' => 'root', ]; //配置数据源DSN信息 //$pdo = new PDO('mysql:host=localhost;dbname=php','root','root'); $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try{ $pdo = new PDO($dsn,$db['username'],$db['password']); echo "连接成功!<br/>"; }catch(PDOException $e){ die( $e->getMessage() ); } ?>
点击 "运行实例" 按钮查看在线实例
<?php session_start(); if (isset($_SESSION['phone'])) { echo '用户: ' . $_SESSION['phone'] . '已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { echo '<a href="dispatch.php?action=login">请登录</a>'; }
点击 "运行实例" 按钮查看在线实例
<?php // 防止用户重复登录 if (isset($_SESSION['phone'])) { echo '<script>alert("不要重复登录");location.assign("index.php");</script>'; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <h3>用户登录</h3> <form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();"> <p> <label for="phone">手机号:</label> <input type="phone" name="phone" id="phone"> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password"> </p> <p> <button>提交</button> </p> </form> <script> function isEmpty() { var phone = document.getElementById('phone').value; var password = document.getElementById('password').value; if (phone.length=== 0 || password.length===0) { alert('手机和密码不能为空'); return false; } } </script> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php // 1.判断用户的请求类型是否正确? if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 2.获取表单数据 $phone = $_POST['phone']; $password = md5($_POST['password']); //var_dump($phone.'--'.$password); // 3. 用用户表user.dbf进行验证 $sql = 'SELECT * FROM `username` WHERE `phone` = :phone AND `password` = :password LIMIT 1'; $stmt = $pdo->prepare($sql); $stmt->execute(['phone'=>$phone, 'password'=>$password]); $user = $stmt->fetch(PDO::FETCH_ASSOC); // 4. 判断验证的结果 if (false === $user) { // 验证失败,返回上一下页面 echo '<script>alert("验证失败");history.back();</script>'; die; } // 验证成功,将用户的信息写到session $_SESSION['phone'] = $user['phone']; echo '<script>alert("登录成功");location.assign("index.php");</script>'; exit; } else { die('请求类型错误'); }
点击 "运行实例" 按钮查看在线实例
<?php // 只需要在该脚本中打开会话即可, check.php/logout.php/login.php都是由它调用的, 不必重复开启 session_start(); // 连接数据库 require __DIR__ . '/connect.php'; // 获取请求参数 $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); // 请求分发 switch ($action) { // 1. 登录页面 case 'login': // 加载登录表单 include __DIR__ . '/login.php'; break; // 2. 验证登录 case 'check': include __DIR__ . '/check.php'; break; // 3. 退出登录 case 'logout': include __DIR__ . '/logout.php'; break; // 默认操作 default: header('Location: index.php'); echo '<script>location.assign("index.php");</script>'; }
点击 "运行实例" 按钮查看在线实例
<?php // 必须在用户已经登录的情况下再退出 if (isset($_SESSION['phone'])) { session_destroy(); echo '<script>alert("退出成功");location.assign("index.php");</script>'; } else { // 要求用户先登录 echo '<script>alert("请先登录");location.assign("index.php");</script>'; }
点击 "运行实例" 按钮查看在线实例