Correction status:qualified
Teacher's comments:评论功能有意思
PDO连接mysql数据库----渲染页面数据
第一步:列出连接数据库所需参数,然后拼接字符串,
$db = [
'type' => 'mysql',
'host' => '127.0.0.1',
'dbname' => 'chen',
'username' => 'root',
'password' => 'root'
];
$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
第二步:实例pdo,连接数据库
try {
$pdo = new PDO($dsn, $db['username'], $db['password']);
} catch (PDOException $e) {
die('数据库连接错误: ' . $e->getMessage());
}
第三步:创建sql语句
$sql3 = 'SELECT * FROM `category`';
第四步:创建预处理对象
$cate_p = $pdo->prepare($sql3);
第五步:执行数据库操作
if ($cate_p->execute()) {
//取出所查询的全部数据
$cates = $cate_p->fetchAll(PDO::FETCH_ASSOC);
} else {
die('<pre>' . print_r($cate_p->errorInfo(), true));
}
运用实例如下:
<?php // 共用文件 function randomkeys($length) { $key=''; $pattern = '1234567890abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLOMNOPQRSTUVWXYZ'; for ($i = 0; $i < $length; $i++) { $key .= $pattern{ mt_rand(0, 35)}; //生成php随机数 } return $key; } $db = [ 'type' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'chen', 'username' => 'root', 'password' => 'root' ]; $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; try { $pdo = new PDO($dsn, $db['username'], $db['password']); } catch (PDOException $e) { die('数据库连接错误: ' . $e->getMessage()); } //电影分类 $sql3 = 'SELECT * FROM `category`'; $cate_p = $pdo->prepare($sql3); if ($cate_p->execute()) { $cates = $cate_p->fetchAll(PDO::FETCH_ASSOC); } else { die('<pre>' . print_r($cate_p->errorInfo(), true)); } //系统设置 $sql = 'SELECT * FROM `system`'; $shuju = $pdo->prepare($sql); if ($shuju->execute()) { $title = $shuju->fetch(PDO::FETCH_ASSOC); } else { die('<pre>' . print_r($shuju->errorInfo(), true)); } //电影详情 $sql2 = 'SELECT * FROM `movies`'; $shuju2 = $pdo->prepare($sql2); if ($shuju2->execute()) { $movies = $shuju2->fetchAll(PDO::FETCH_ASSOC); } else { die('<pre>' . print_r($shuju2->errorInfo(), true)); } //添加评论 if ($_SERVER['REQUEST_METHOD'] != 'POST') { echo json_encode(array( 'code' => 111, )); } else { $img = 'gir.gif'; $name = randomkeys(10); $textarea = $_POST['textarea']; $mov_id = $_POST['moc_id']; $cate_id = $_POST['cate_id']; $sql4 = 'INSERT INTO `COMMENT` SET `mov_id`=:mov_id,`name`=:name,`image`=:image,`detail`=:detail,`cate_id`=:cate_id'; $shuju4 = $pdo->prepare($sql4); $shuju4->bindParam('mov_id', $mov_id, PDO::PARAM_INT); $shuju4->bindParam('name', $name, PDO::PARAM_STR); $shuju4->bindParam('image', $img, PDO::PARAM_STR); $shuju4->bindParam('detail', $textarea, PDO::PARAM_STR); $shuju4->bindParam('cate_id', $cate_id, PDO::PARAM_INT); if ($shuju4->execute()) { if($shuju4->rowCount()>0){ echo json_encode( array( 'code'=>222, 'img'=>$img, 'name'=>$name, 'textarea'=>$textarea )); } } else { die('<pre>' . print_r($shuju4->errorInfo(), true)); } }
点击 "运行实例" 按钮查看在线实例
<?php include_once __DIR__.'/common.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="static/css/style1.css"> <title><?php echo $title['copy'].'的'.$title['title']; ?></title> </head> <body> <!--头部导航--> <div class="header"> <ul class="nav"> <li><a href="index.php">首页</a></li> <?php foreach ($cates as $cate) : ?> <li><a href="<?php echo$cate['name']; ?>.php?cate_id=<?php echo $cate['cate_id']; ?>"><?php echo $cate['alias'] ?></a></li> <?php endforeach; ?> </ul> </div> <?php // 加载公共头部 include __DIR__ . '/header.php'; $mov_id = intval($_GET['mov_id']); foreach ($movies as $movie) { if ($movie['mov_id'] == $mov_id) { echo '<h3 cate_id="'.$movie['cate_id'].'">'.$movie['name'].'</h3>'; echo '<img src="/0920/static/img/' . $movie['image'] . '" alt="" width="300">'; echo "<p style='text-indent: 2em'>{$movie['detail']}</p>"; } }; $sql3 = 'SELECT * FROM `comment`'; $shuju3 = $pdo->prepare($sql3); if ($shuju3->execute()) { $comment = $shuju3->fetchAll(PDO::FETCH_ASSOC); } else { die('<pre>' . print_r($shuju3->errorInfo(), true)); } echo '<div class="comment"> <h3>观影评论:</h3> <ul>'; foreach ($comment as $k => $v) { if ($v['mov_id'] == $mov_id) { $cate_id=$v['cate_id']; echo '<li> <img src="/0920/static/img/' . $v['image'] . '"> <span>' . $v['name'] . '</span> <p>' . $v['detail'] . '</p> </li>'; } } echo '</ul><textarea name="desc" id="tt1">一时摸鱼一时爽,一直摸鱼一直爽!</textarea><br> <button type="button" onclick="click_cc();">提交</button> </div>'; ?> <script type="text/javascript" src="static/js/jquery-2.2.4.min.js"></script> <script type="text/javascript"> function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null; } function click_cc() { var textarea = $('#tt1').val(); var moc_id = getQueryString('mov_id'); var cate_id = $('h3[cate_id]').attr('cate_id'); $.post("common.php",{"textarea":textarea,"moc_id":moc_id,"cate_id":cate_id},function(res){ if(res.code == 222){ var comment = '<li><img src="/0920/static/img/'+res***g+'"><span>'+res.name+'</span><p>' + res.textarea + '</p></li>'; $('.comment>ul').append(comment); confirm('评论保存成功 O!^O^!O'); }else{ alert('评论保存失败 ~~T_T~~'); } },"json"); } </script> <!--底部版权--> <div class="footer"> <p class="copyright"><?php echo $title['copy']; ?> © 版权所有</p> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例