防spl注入添加
<?php
$name = $_POST['name'];
$text = $_POST['text'];
$static = $_POST['static'];
$dbh = new \PDO("mysql:host=127.0.0.1;dbname=my6","root","root");
if (!empty($name)) {
$sql = "INSERT INTO `article` (`time` ,`text`,`style`)VALUES (:login, :password,:style)";
$stmt = $dbh->prepare($sql); $stmt->execute(array(':login'=>$name,':password'=>$text,':style'=>$static));
echo "ok";
}
?>
PDO查询展示数据
<?php
$dbms = 'mysql';
$host = 'localhost';
$dbName = 'my6';
$user = 'root';
$pass = 'root';
$dsn = "$dbms:host=$host;dbname=$dbName";
$dbh = new PDO($dsn, $user, $pass);
$sql = "SELECT * FROM `article`";
$stmt = $dbh->query($sql);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php foreach ($row as $value):?>
<?php echo $value['id']; ?>
<div>
<a href="day30p.php?aid=<?php echo $value['id']?>"><?php echo $value['time']?></a>
</div>
<?php endforeach; ?>
</body>
</html>
生成静态文件
if判断数据库里面的 style字段是否为1 如是1 则执行静态文件
<?php
$id = $_GET['aid'];
$dbh = new \PDO("mysql:host=127.0.0.1;dbname=my6","root","root");
$sql="select * from article WHERE `id` = :login ";
$stmt = $dbh->prepare($sql);
$stmt->execute(array(':login'=>$id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['style'];
if ($row['style']==1){
ob_start();
$file = ob_get_contents();
$f_file =fopen("article_$id.html","w");
fwrite($f_file,$file);
fclose($f_file);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<label>标题</label>
<div>
<?php echo $row['time']?>
</div>
</div>
<div>
<label>内容</label>
<div>
<?php echo $row['text']?>
</div>
</div>
</body>
</html>
|