簡單表白牆的實現

表白牆主要功能包括:發佈以及評論。

數據庫結構

數據表love
love
數據表comment
comment

首頁

公共函數文件 tool.php

<?php
function get($name){
    return isset($_GET)?$_GET['id']:"";
}
function post($name){
    return isset($_POST)?$_POST['content']:"";
}

function conn(){
    //數據庫驅動類型:host=主機名;dbname=數據庫
    $dns = "mysql:host=localhost;dbname=noxue";
    //連接字符串,賬號,密碼
    return new PDO($dns,"root","root");
}

首頁 index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表白牆</title>
    <style type="text/css">
        .item{
            background: #dddddd;
            margin: 20px;
        }
        .item p+p{
            color: #999999;
        }
    </style>
</head>
<body>
<a href="./love.html">假裝發一個表白</a>
<?php
require_once "./tool.php";

$db=conn();
$sql='select * from love';
$res = $db->query($sql);
foreach ($res as $row){
    ?>
    <div class="item">
        <p><?php
            echo substr($row['content'],0,20);
            echo "...<a href='./content.php?id=".$row['id']."'>詳細內容</a>"?></p>
        <p><?=$row['created_at']; ?></p>
    </div>
    <?php
}
?>
</body>
</html>

發表功能

love.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>發佈表白</title>
</head>
<body>
<form action="love.php" method="post">
    <p>
        <textarea name="content" id="" cols="30" rows="10" placeholder="請輸入表白信息"></textarea>
    </p>
    <p>
        <input type="submit" value="發佈表白">
    </p>
</form>
</body>
</html>

love.php

<?php
require_once "./tool.php";
//獲取數據庫連接
$content = post('content');
$sql = 'insert into love(content) VALUES("'.$content.'")';
echo $sql;
$db=conn();
//執行查詢操作
$count=$db->exec($sql);
echo $count;

$db=null;
//跳轉到首頁
header('Location: index.php');

詳細內容

content.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表白內容</title>
    <style type="text/css">

    </style>
</head>
<body>
<?php
require_once './tool.php';
$id=get('id');

$db=conn();
$sql = 'select * from love WHERE id='.$id;
$res = $db->query($sql);
$love=null;
foreach($res as $row){
    $love=$row;
    break;
}
//print_r($love);
?>
<div class="content">
    <p><?=$love['created_at']?></p>
    <p><?=$love['content']?></p>
</div>
<form action="comment.php" method="post">
    <input type="hidden"name="id" value="<?=$love['id']?>">
    <p><textarea name="content" id="" cols="30" rows="10"></textarea></p>
    <p><input type="submit" value="發佈評論"></p>
</form>
<?php
$sql='select * from comment WHERE love_id='.$id;
$res= $db->query($sql);
foreach($res as $row){
    ?>
    <div class="comment-item">
        <p><?=$row['created_at']?></p>
        <p><?=$row['content']?></p>
    </div>
<?php
}

?>
</body>
</html>

發表評論功能

comment.php

<?php
require_once 'tool.php';

$id = isset($_POST)?$_POST['id']:"";
$content = post('content');

$sql = 'insert into comment(love_id,content,created_at) VALUES ('.$id.',"'.$content.'",now())';
echo $sql;
$db = conn();
$count=$db->exec($sql);
header("Location: content.php?id=".$id);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章