php pdo文章发布,列表,查看

完整代码打包:

blog.rar

数据表结构

DROP TABLE IF EXISTS `articles`;
CREATE TABLE IF NOT EXISTS `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8_bin NOT NULL,
  `content` text COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
  • add.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>发布文章</title>
</head>

<body>
    <h1>发布文章</h1>
    <form action="add.php" method="post">
        <p>标题:<input type="text" name="title"></p>
        <p>内容:<br /><textarea name="content" id="" cols="30" rows="10"></textarea></p>
        <p><input type="submit" value="发布文章"></p>
    </form>
</body>

</html>
  • add.php
<?php

echo $_POST["title"];


echo $_POST["content"];



// try-catch语句 处理连接错误信息
try {
    // 创建 pdo 对象,连接数据库
    $pdo = new pdo("mysql:host=localhost;dbname=blog", "root", "root");

    // 设置编码和数据库统一,解决数据乱码
    $pdo->exec("SET CHARACTER SET utf8");
} catch (PDDException $e) {
    echo "数据库连接错误";
}

// 预编译 sql 语句
$stmt = $pdo->prepare("insert into articles(title, content) values(:title, :content)"); 

// 绑定参数值
$stmt->bindParam(":title", $_POST["title"], PDO::PARAM_STR);
$stmt->bindParam(":content", $_POST["content"], PDO::PARAM_STR);

// 执行 sql 语句
$stmt->execute();

// 添加后跳转到 列表页面
header("location:list.php");
  • list.php
<?php


// try-catch语句 处理连接错误信息
try {
    // 创建 pdo 对象,连接数据库
    $pdo = new pdo("mysql:host=localhost;dbname=blog", "root", "root");

    // 设置编码和数据库统一,解决数据乱码
    $pdo->exec("SET CHARACTER SET utf8");
} catch (PDDException $e) {
    echo "数据库连接错误";
}

// 预编译 sql 语句
$stmt = $pdo->prepare("select * from articles order by id desc"); 

// 执行 sql 语句
$stmt->execute();

$result = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div><a href="add.html">添加文章</a></div>
    <ul>
    <?php 
    foreach($result as $row){
    ?>
        <li><a href="view.php?id=<?=$row['id'] ?>"> <?=$row['title'] ?></a></li>
    <?php
    }
    ?>
    </ul>
</body>
</html>
  • view.php
<?php



// try-catch语句 处理连接错误信息
try {
    // 创建 pdo 对象,连接数据库
    $pdo = new pdo("mysql:host=localhost;dbname=blog", "root", "root");

    // 设置编码和数据库统一,解决数据乱码
    $pdo->exec("SET CHARACTER SET utf8");
} catch (PDDException $e) {
    echo "数据库连接错误";
}

// 预编译 sql 语句
$stmt = $pdo->prepare("select * from articles where id=:id"); 

$stmt->bindParam(":id", $_GET['id'], PDO::PARAM_INT);
// 执行 sql 语句
$stmt->execute();

$result = $stmt->fetch();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1><?=$result['title'] ?></h1>
    <div><?=$result['content'] ?></div>
</body>
</html>